/// <summary> /// Tries to fill the document with data returning true if field count matched /// </summary> public static bool TryFillFromJSON(Doc doc, IJsonDataObject jsonData, SetFieldFunc setFieldFunc = null) { if (doc == null || jsonData == null) { return(false); } var allMatch = true; var map = jsonData as JsonDataMap; if (map != null) { foreach (var kvp in map) { var fdef = doc.Schema[kvp.Key]; if (fdef == null) { var ad = doc as IAmorphousData; if (ad != null && ad.AmorphousDataEnabled) { ad.AmorphousData[kvp.Key] = kvp.Value; } allMatch = false; continue; } if (setFieldFunc == null) { doc.SetFieldValue(fdef, kvp.Value); } else { var ok = setFieldFunc(doc, fdef, kvp.Value); if (!ok) { allMatch = false; } } } if (map.Count != doc.Schema.FieldCount) { allMatch = false; } } else { var arr = jsonData as JsonDataArray; if (arr == null) { return(false); } for (var i = 0; i < doc.Schema.FieldCount; i++) { if (i == arr.Count) { break; } var fdef = doc.Schema[i]; if (setFieldFunc == null) { doc.SetFieldValue(fdef, arr[i]); } else { var ok = setFieldFunc(doc, fdef, arr[i]); if (!ok) { allMatch = false; } } } if (arr.Count != doc.Schema.FieldCount) { allMatch = false; } } return(allMatch); }
public JsonDynamicObject(IJsonDataObject data) { m_Data = data; }