I've been successfully writing and parsing back (recursively) my data using a custom JsonConverter, using this:
public override void WriteJson(JsonWriter writer, ManagedSubData value, JsonSerializer serializer)
{
skipOverMe = true;
value.GrabAssetPath();
writer.WriteValue(JsonConvert.SerializeObject(value));
}
The problem is, this gives me an unreadable Json full of backslashes everywhere. Now, I can use this instead of WriteValue, and it outputs a nice readable Json:
writer.WriteRawValue(JsonConvert.SerializeObject(value));
But when I try to read it back, it fails. reader.Value
is now null
, it's like it failed before even calling ReadJson
public override ManagedSubData ReadJson(JsonReader reader, Type objectType, ManagedSubData existingValue,
bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
//fails here since reader.Value is null
JObject obj = JObject.Parse(reader.Value.ToString());
ManagedSubData asset = ManagedDataHandler.GetSubDataFromGUID(obj["GUID"].ToString());
if (asset == null)
{
asset = ScriptableObject.CreateInstance(objectType) as ManagedSubData;
#if UNITY_EDITOR
if (!Application.isPlaying)
{
AssetDatabase.CreateAsset(asset, obj["assetPath"].ToString());
AssetDatabase.SaveAssets();
}
#endif
}
if (!asset.KeepPlayModeChanges)
{
JsonConvert.PopulateObject(reader.Value.ToString(), asset);
asset.SetDirty();
}
return asset;
}
There are actual field values if I try to do reader.Read
, it's just that the Value of the object itself is considered null
.
Maybe I shouln't be trying to write Raw value? I am noticing that for every level of nesting, WriteValue adds additional slashes, but maybe it needs those to parse it back?
Does anyone have any ideas? We kinda need the Json to be readable and not full of garbage.