/// <summary> /// Compose data to JSON data /// </summary> /// <returns></returns> public IJSONItem ToJSON(IJSONDocument doc) { string output = string.Empty; IJSONItemObject o = doc.CreateItemObject(); o.Add("handler", handler.ToString()); o.Add("propertyName", propName); return(o); }
/// <summary> /// load /// </summary> /// <param name="dest"></param> /// <param name="src"></param> private void LoadObject(object dest, IJSONItemObject src) { // setup var loadableDest = dest as IJSONLoadableObject; if (loadableDest != null) { loadableDest.OnBeforeLoad(); } bool clsMandatory = AllMandatory; var clsLoadable = Loadable; var clsAttr = (JSONLoadableClassAttribute)Attribute.GetCustomAttribute(dest.GetType(), typeof(JSONLoadableClassAttribute)); if (clsAttr != null) { clsMandatory = clsAttr.AllMandatory; clsLoadable = clsAttr.Loadable; } // all fields var fields = dest.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var field in fields) { var fieldAttr = (JSONLoadableElementAttribute)field.GetCustomAttribute(typeof(JSONLoadableElementAttribute)); var fieldLoadable = clsLoadable; string extName = field.Name;; bool fieldMandatory = clsMandatory; if (fieldAttr != null) { fieldMandatory = fieldAttr.IsMandatory; if (fieldAttr.ExternalName != null) { extName = fieldAttr.ExternalName; } } if (!(((clsLoadable & JSONLoadableType.Loadable) != 0 && fieldAttr != null) || // skip loadable ((clsLoadable & JSONLoadableType.PublicFields) != 0 && field.IsPublic) || // skip public field ((clsLoadable & JSONLoadableType.NonPublicFields) != 0 && !field.IsPublic))) // skip a nonPublic field { continue; } IJSONItem item; if (!src.TryGetValue(extName, out item)) { if (fieldMandatory) { throw new ArgumentException("missing mandatory field '" + extName + "'"); } continue; } var fieldValue = MapItem(item, field.FieldType); if (loadableDest == null || loadableDest.OnSetField(field, fieldValue, out fieldValue)) { field.SetValue(dest, fieldValue); } } // all properties var properties = dest.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var property in properties) { var propertyAttr = (JSONLoadableElementAttribute)property.GetCustomAttribute(typeof(JSONLoadableElementAttribute)); var propertyLoadable = clsLoadable; string extName = property.Name;; bool propertyMandatory = clsMandatory; if (propertyAttr != null) { propertyMandatory = propertyAttr.IsMandatory; if (propertyAttr.ExternalName != null) { extName = propertyAttr.ExternalName; } } bool isPublic = property.GetMethod.IsPublic; if (!(((clsLoadable & JSONLoadableType.Loadable) != 0 && propertyAttr != null) || // skip loadable ((clsLoadable & JSONLoadableType.PublicProperties) != 0 && isPublic) || // skip public field ((clsLoadable & JSONLoadableType.NonPublicProperties) != 0 && !isPublic))) // skip a nonPublic field { continue; } IJSONItem item; if (!src.TryGetValue(extName, out item)) { if (propertyMandatory) { throw new ArgumentException("missing mandatory field '" + extName + "'"); } continue; } var propertyValue = MapItem(item, property.PropertyType); if (loadableDest == null || loadableDest.OnSetProperty(property, propertyValue, out propertyValue)) { property.SetValue(dest, propertyValue); } } if (loadableDest != null) { loadableDest.OnAfterLoad(); } }