internal static IntermediateField Deserialize(IntermediateFieldDefinition definition, Uri baseUri, JsonObject jsonObject) { var field = new IntermediateField(definition); if (jsonObject != null) { field.Value = IntermediateFieldValue.Deserialize(jsonObject, baseUri, definition.Type); } return(field); }
internal static IntermediateFieldValue Deserialize(JsonObject jsonObject, Uri baseUri, IntermediateFieldType type) { var context = jsonObject.GetValueOrDefault <string>("context"); if (!jsonObject.TryGetValue("data", out var data)) { throw new ArgumentException(); } var value = data; if (data is string) { } else if (data is long) { if (type == IntermediateFieldType.Number) { value = Convert.ToInt32(data); } } else if (data is JsonObject jsonData) { jsonData.TryGetValue("embeddedIndex", out var embeddedIndex); value = new IntermediateFieldPathValue { BaseUri = (embeddedIndex == null) ? null : baseUri, EmbeddedFileIndex = (embeddedIndex == null) ? null : (int?)Convert.ToInt32(embeddedIndex), Path = jsonData.GetValueOrDefault <string>("path"), }; } var previousValueJson = jsonObject.GetValueOrDefault <JsonObject>("prev"); var previousValue = (previousValueJson == null) ? null : IntermediateFieldValue.Deserialize(previousValueJson, baseUri, type); return(new IntermediateFieldValue { Context = context, Data = value, PreviousValue = previousValue }); }
internal static IntermediateFieldValue Deserialize(JsonObject jsonObject, Uri baseUri, IntermediateFieldType type) { var context = jsonObject.GetValueOrDefault <string>("context"); if (!jsonObject.TryGetValue("data", out var data)) { throw new ArgumentException(); } var value = data; switch (value) { case int intData: switch (type) { case IntermediateFieldType.Bool: value = intData != 0; break; case IntermediateFieldType.LargeNumber: value = Convert.ToInt64(data); break; case IntermediateFieldType.Path: case IntermediateFieldType.String: value = intData.ToString(); break; } break; case long longData: switch (type) { case IntermediateFieldType.Bool: value = longData != 0; break; case IntermediateFieldType.Number: value = Convert.ToInt32(longData); break; case IntermediateFieldType.Path: case IntermediateFieldType.String: value = longData.ToString(); break; } break; case JsonObject jsonData: jsonData.TryGetValue("embed", out var embed); value = new IntermediateFieldPathValue { BaseUri = (embed != null) ? baseUri : null, Embed = embed != null, Path = jsonData.GetValueOrDefault <string>("path"), }; break; // Nothing to do for this case, so leave it out. // case string stringData: // break; } var previousValueJson = jsonObject.GetValueOrDefault <JsonObject>("prev"); var previousValue = (previousValueJson == null) ? null : IntermediateFieldValue.Deserialize(previousValueJson, baseUri, type); return(new IntermediateFieldValue { Context = context, Data = value, PreviousValue = previousValue }); }