public static void copyFields(DataObject o1, DataObject o2, bool copy = false) { string json = toJson(o1); JsonSerializer serializer = new JsonSerializer(); serializer.write = false; serializer.copyGuids = copy; serializer.obj = new JsonObject(json); serializer.readObjectBody(() => { o2.AddFields(serializer); }, serializer.obj); if (copy) { serializer.UpdateReferences(); } }
private void writeObject(DataObject data, String key, Type type) { if (data == null) { if (key == null) { writer.nul(); } else { writer.nul(key); } return; } int index = seenObjectIndex(data); if (index >= 0) { if (key == null) { writer.value(index); } else { writer.value(key, index); } return; } index = seenObjects.Count; seenObjects.Add(data); writeObjectJson(key, () => { writer.value(makeKey(CLASS), className(type)); writer.value(makeKey(ID), index); if (data is GuidDataObject) { context.Add(data as GuidDataObject); } data.AddFields(this); }); }
private DataObject read(JsonObject obj, bool skipUnknownClasses = false) { if (obj == null) { return(null); } if (!obj.containsKey(ID)) { return(null); } // First check to see if it's been created, and if so simply return it int id = obj.getInt(ID); if (id < seenObjects.Count) { DataObject seen = seenObjects[id]; if (seen != null) { return(seen); } } // Create the (unpopulated) object and register it String className = obj.getString(CLASS); if (className == null) { return(null); } DataObject data = Constructor.Construct(className, skipUnknownClasses); while (seenObjects.Count <= id) { seenObjects.Add(null); } seenObjects[id] = data; // Instantiate the children of this object in the JSON file // This is done to create objects whose parents may no longer // exist in the data-structure, but who are themselves still // referenced. This occurs when fields are removed from a class. foreach (var kvp in obj.Entries) { object value = kvp.Value; List <object> list = value as List <object>; if (list != null) { foreach (object entry in list) { read(entry as Dictionary <string, object>, true); } } read(value as Dictionary <string, object>, true); } // If there's not corresponding C# class, we stop here. // This may happen if the class was deleted, but its data // is still in an older JSON file. if (data == null) { return(null); } // Now actually populate the object, having guaranteed // that all its children have been created (but not // necessarily populated!) readObjectBody(() => { makeKey(CLASS); makeKey(ID); data.AddFields(this); if (data is GuidDataObject) { context.Add(data as GuidDataObject); } }, obj); return(data); }