private static bool getEmbedField(JSONField fieldJSON, out EmbedFieldBuilder field) { if (fieldJSON.Container.TryGetField(NAME, out string name) && fieldJSON.Container.TryGetField(VALUE, out string value)) { fieldJSON.Container.TryGetField(INLINE, out bool isInline, false); if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(value)) { if (name.Length > EMBEDFIELDNAME_MAX) { throw new EmbedParseException($"A field name may not exceed {EMBEDFIELDNAME_MAX} characters!"); } if (value.Length > EMBEDFIELDVALUE_MAX) { throw new EmbedParseException($"A field value may not exceed {EMBEDFIELDVALUE_MAX} characters!"); } field = new EmbedFieldBuilder() { Name = name, Value = value, IsInline = isInline }; return(true); } } field = null; return(false); }
public static object[] GetJSONFieldInstances(object obj) { JSONField[] JSONFields = GetJSONFields(obj.GetType()); List <object> fieldInstances = new List <object>(); foreach (JSONField JSONField in JSONFields) { if (JSONField.GetFieldType().IsArray) { object[] array = (object[])JSONField.GetValue(obj); if (array != null) { fieldInstances.AddRange(array); } } else { object newObj = JSONField.GetValue(obj); if (newObj != null) { fieldInstances.Add(newObj); } } } return(fieldInstances.ToArray()); }
public static JSONField[] GetJSONFields(Type objType) { //Find all fields in type that have been marked with the JSONFieldAttribute List <JSONField> JSONFields = new List <JSONField>(); BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy; //First find fields FieldInfo[] fields = objType.GetFields(bindingAttr); foreach (FieldInfo field in fields) { JSONFieldAttribute fieldAttribute = SystemUtils.GetAttribute <JSONFieldAttribute>(field); if (fieldAttribute != null) { JSONField JSONField = new JSONField(fieldAttribute, field); JSONFields.Add(JSONField); } } //Then find all properties PropertyInfo[] properties = objType.GetProperties(bindingAttr); foreach (PropertyInfo property in properties) { JSONFieldAttribute fieldAttribute = SystemUtils.GetAttribute <JSONFieldAttribute>(property); if (fieldAttribute != null) { JSONField JSONField = new JSONField(fieldAttribute, property); JSONFields.Add(JSONField); } } return(JSONFields.ToArray()); }
public static bool FindJSONField(Type objType, string id, out JSONField field) { JSONField[] JSONFields = GetJSONFields(objType); foreach (JSONField JSONField in JSONFields) { if (JSONField.GetID() == id) { field = JSONField; return(true); } } field = new JSONField(); return(false); }
private static bool ShouldWriteObject(Type objType, ObjectConverter converter, object obj, object defualtObj) { //If its an array always write if (objType.IsArray) { return(true); } //Never write the object if it's null (??what about if the default is non null?? write a null node??) else if (obj == null) { return(false); } //If object is null by default OR is of a different then always write else if (defualtObj == null || defualtObj.GetType() != obj.GetType()) { return(true); } //If the object has a converter ask it if should write else if (converter != null) { return(converter._shouldWrite(obj, defualtObj)); } //otherwise check objects fields to see if they need to write else { JSONField[] JSONFields = GetJSONFields(obj.GetType()); foreach (JSONField JSONField in JSONFields) { ObjectConverter fieldConverter = GetConverter(JSONField.GetFieldType()); object fieldObj = JSONField.GetValue(obj); object defualtFieldObj = JSONField.GetValue(defualtObj); if (ShouldWriteObject(JSONField.GetFieldType(), fieldConverter, fieldObj, defualtFieldObj)) { return(true); } } } return(false); }
public IParserData GetData(int index) { IParserData jSONData = (IParserData)Activator.CreateInstance(_dataType); JSONNode progNode = _nodeList[index]; if (progNode != null) { for (int i = 0; i < _data.Fields.Count; i++) { JSONField field = _data.Fields[i]; List <JSONNode> subNode = progNode.GetNodes(field.JSONName, null); if (subNode.Count > 0) { jSONData.SetElement(field.FieldName, subNode[0].GetValue("")); } } } return(jSONData); }
public static object FromJSONElement(Type objType, JSONElement node, object defualtObject = null) { object obj = null; //If object is an array convert each element one by one if (objType.IsArray) { JSONArray jsonArray = (JSONArray)node; int numChildren = node != null ? jsonArray._elements.Count : 0; //Create a new array from this nodes children Array array = Array.CreateInstance(objType.GetElementType(), numChildren); for (int i = 0; i < array.Length; i++) { //Convert child node object elementObj = FromJSONElement(objType.GetElementType(), jsonArray._elements[i]); //Then set it on the array array.SetValue(elementObj, i); } //Then set value on member obj = array; } else { //First find the actual object type from the JSONElement (could be different due to inheritance) Type realObjType = GetRuntimeType(node); //If the JSON node type and passed in type are both generic then read type from runtime type node if (NeedsRuntimeTypeInfo(objType, realObjType)) { realObjType = ReadTypeFromRuntimeTypeInfo(node); //If its still can't be found then use passed in type if (realObjType == null) { realObjType = objType; } } //If we don't have an JSONElement or the object type is invalid then use passed in type else if (node == null || realObjType == null || realObjType.IsAbstract || realObjType.IsGenericType) { realObjType = objType; } //Convert objects fields if (defualtObject != null) { obj = defualtObject; } //Create an object instance if default not passed in else if (!realObjType.IsAbstract) { obj = CreateInstance(realObjType); } //If the object has an associated converter class, convert the object using it ObjectConverter converter = GetConverter(realObjType); if (converter != null) { obj = converter._onConvertFromJSONElement(obj, node); } //Otherwise convert fields else if (node != null && obj != null) { JSONField[] JSONFields = GetJSONFields(realObjType); foreach (JSONField JSONField in JSONFields) { //First try and find JSON node with an id attribute matching our attribute id JSONElement fieldNode = JSONUtils.FindChildWithAttributeValue(node, kJSONFieldIdAttributeTag, JSONField.GetID()); object fieldObj = JSONField.GetValue(obj); Type fieldObjType = JSONField.GetFieldType(); //Convert the object from JSON node fieldObj = FromJSONElement(fieldObjType, fieldNode, fieldObj); //Then set value on parent object try { JSONField.SetValue(obj, fieldObj); } catch (Exception e) { throw e; } } } } //IJSONConversionCallbackReceiver callback if (obj is IJSONConversionCallbackReceiver) { ((IJSONConversionCallbackReceiver)obj).OnConvertFromJSONElement(node); } return(obj); }
//The equivalent of a SerializedPropertyField but for objects serialized using JSON. public static T ObjectField <T>(T obj, GUIContent label, out bool dataChanged) { //If object is an array show an editable array field if (typeof(T).IsArray) { bool arrayChanged = false; Array arrayObj = obj as Array; arrayObj = ArrayField(label, arrayObj, typeof(T).GetElementType(), ref arrayChanged); if (arrayChanged) { dataChanged = true; return((T)(arrayObj as object)); } dataChanged = false; return(obj); } //If the object is a IJSONCustomEditable then just need to call its render properties function. if (obj != null && obj is IJSONCustomEditable) { dataChanged = ((IJSONCustomEditable)obj).RenderObjectProperties(label); return(obj); } Type objType = obj == null ? typeof(T) : obj.GetType(); //Otherwise check the class has a object editor class associated with it. JSONEditorAttribute.RenderPropertiesDelegate renderPropertiesDelegate = GetEditorDelegateForObject(objType); if (renderPropertiesDelegate != null) { //If it has one then just need to call its render properties function. return((T)renderPropertiesDelegate(obj, label, out dataChanged)); } //Otherwise loop through each JSON field in object and render each as a property field { dataChanged = false; JSONField[] JSONFields = JSONConverter.GetJSONFields(objType); foreach (JSONField JSONField in JSONFields) { if (!JSONField.HideInEditor()) { //Create GUIContent for label and optional tooltip string fieldName = StringUtils.FromCamelCase(JSONField.GetID()); TooltipAttribute fieldToolTipAtt = SystemUtils.GetAttribute <TooltipAttribute>(JSONField); GUIContent labelContent = fieldToolTipAtt != null ? new GUIContent(fieldName, fieldToolTipAtt.tooltip) : new GUIContent(fieldName); bool fieldChanged; object nodeFieldObject = JSONField.GetValue(obj); if (JSONField.GetFieldType().IsArray) { fieldChanged = false; nodeFieldObject = ArrayField(labelContent, nodeFieldObject as Array, JSONField.GetFieldType().GetElementType(), ref fieldChanged); } else { nodeFieldObject = ObjectField(nodeFieldObject, labelContent, out fieldChanged); } if (fieldChanged) { dataChanged = true; JSONField.SetValue(obj, nodeFieldObject); } } } return(obj); } }