private void ReadBase(object obj, Dictionary <string, object> fields, ref JsonOutputObject outputJsonObject) { Type objType = Type.GetType(fields[fieldType].ToString()); foreach (string fieldName in fields.Keys) { if (!IsSpecialField(fieldName)) { object value = fields[fieldName]; FieldInfo field = objType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (IsRef(value)) { LinkToField newLink = new LinkToField(); newLink.Obj = obj; newLink.Field = field; newLink.RefIndex = ParseRef(value.ToString()); outputJsonObject.Links.Add(newLink); } else { field.SetValue(obj, ConvertValue(value, field.FieldType)); } } } }
private void CreateObject(Dictionary <string, object> fields, ref JsonOutputObject outputJsonObject) { if (!fields.ContainsKey(fieldType)) { return; } if (fields.ContainsKey(fieldRef)) { outputJsonObject.RefIndex = ToRefIndex(fields[fieldRef]); } else { outputJsonObject.RefIndex = 0; } Type objType = Type.GetType(fields[fieldType].ToString()); if (objType == null) { return; } object obj = Activator.CreateInstance(objType); foreach (string fieldName in fields.Keys) { if (!IsSpecialField(fieldName)) { object value = fields[fieldName]; FieldInfo field = objType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (IsRef(value)) { LinkToField newLink = new LinkToField(); newLink.Obj = obj; newLink.Field = field; newLink.RefIndex = ParseRef(value.ToString()); outputJsonObject.Links.Add(newLink); } else { field.SetValue(obj, ConvertValue(value, field.FieldType)); } } } if (fields.ContainsKey(fieldBase)) { ReadBase(obj, fields[fieldBase] as Dictionary <string, object>, ref outputJsonObject); } outputJsonObject.Object = obj; }
public object Deserialize(Stream serializationStream) { ObjectsBufferLoad objectsBuffer = new ObjectsBufferLoad(); JsonTextReader reader = new JsonTextReader(new StreamReader(serializationStream)); try { while (reader.Read() && reader.TokenType != JsonToken.EndObject) { if (reader.TokenType == JsonToken.PropertyName) { if (reader.Value.Equals(fieldRoot) && reader.Read()) { objectsBuffer.RootRef = ToRefIndex(reader.Value); } else if (reader.Value.Equals(fieldArrayElements) && reader.Read()) { while (reader.Read() && reader.TokenType != JsonToken.EndArray) { if (reader.TokenType == JsonToken.StartObject) { JsonOutputObject outputObj = ReadJsonObject(reader); objectsBuffer.Objects.Add(outputObj.RefIndex, outputObj.Object); foreach (Link link in outputObj.Links) { objectsBuffer.Links.Add(link); } } } } } } foreach (Link link in objectsBuffer.Links) { object value; if (link is LinkToField) { LinkToField linkToField = link as LinkToField; if (objectsBuffer.Objects.TryGetValue(linkToField.RefIndex, out value)) { try { linkToField.Field.SetValue(link.Obj, value); } catch (Exception e) { } } } else if (link is LinkToArrayEl) { LinkToArrayEl linkToArrayEl = link as LinkToArrayEl; Array arr = linkToArrayEl.Obj as Array; if (objectsBuffer.Objects.TryGetValue(link.RefIndex, out value)) { try { arr.SetValue(value, linkToArrayEl.Indexes); } catch (Exception e) { } } } } return(objectsBuffer.RootObject); } catch (Exception) { return(null); } }