public static object OnConvertFromJSONElement(object obj, JSONElement node) { if (node == null) { return(obj); } AnimationCurve curve = new AnimationCurve(); JSONElement keyframesJSONElement = JSONUtils.FindChildWithAttributeValue(node, JSONConverter.kJSONFieldIdAttributeTag, "keyFrames"); if (keyframesJSONElement != null) { List <Keyframe> keyFrames = new List <Keyframe>(); foreach (JSONElement child in keyframesJSONElement.ChildNodes) { Keyframe keyFrame = new Keyframe(); keyFrame.inTangent = JSONConverter.FieldObjectFromJSONElement <float>(child, "inTangent"); keyFrame.outTangent = JSONConverter.FieldObjectFromJSONElement <float>(child, "outTangent"); keyFrame.tangentMode = JSONConverter.FieldObjectFromJSONElement <int>(child, "tangentMode"); keyFrame.time = JSONConverter.FieldObjectFromJSONElement <float>(child, "time"); keyFrame.value = JSONConverter.FieldObjectFromJSONElement <float>(child, "value"); keyFrames.Add(keyFrame); } curve.keys = keyFrames.ToArray(); } return(curve); }
private static Type ReadTypeFromRuntimeTypeInfo(JSONElement node) { Type type = null; if (node != null) { JSONElement childJSONElement = JSONUtils.FindChildWithAttributeValue(node, kJSONElementRuntimeTypeAttributeTag, ""); if (childJSONElement != null) { type = (Type)FromJSONElement(typeof(Type), childJSONElement); } } return(type); }
public static object OnConvertFromJSONElement(object obj, JSONElement node) { if (node == null) { return(obj); } Gradient gradient = new Gradient(); JSONElement colorKeysJSONElement = JSONUtils.FindChildWithAttributeValue(node, JSONConverter.kJSONFieldIdAttributeTag, "colorKeys"); if (colorKeysJSONElement != null) { List <GradientColorKey> colorKeys = new List <GradientColorKey>(); foreach (JSONElement child in colorKeysJSONElement.ChildNodes) { GradientColorKey colorKey = new GradientColorKey(); colorKey.color = JSONConverter.FieldObjectFromJSONElement <Color>(child, "color"); colorKey.time = JSONConverter.FieldObjectFromJSONElement <float>(child, "time"); colorKeys.Add(colorKey); } gradient.colorKeys = colorKeys.ToArray(); } JSONElement alphaKeysJSONElement = JSONUtils.FindChildWithAttributeValue(node, JSONConverter.kJSONFieldIdAttributeTag, "alphaKeys"); if (alphaKeysJSONElement != null) { List <GradientAlphaKey> alphaKeys = new List <GradientAlphaKey>(); foreach (JSONElement child in alphaKeysJSONElement.ChildNodes) { GradientAlphaKey alphaKey = new GradientAlphaKey(); alphaKey.alpha = JSONConverter.FieldObjectFromJSONElement <float>(child, "alpha"); alphaKey.time = JSONConverter.FieldObjectFromJSONElement <float>(child, "time"); alphaKeys.Add(alphaKey); } gradient.alphaKeys = alphaKeys.ToArray(); } return(gradient); }
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); }
public static T FieldObjectFromJSONElement <T>(JSONElement parentNode, string id, T defualtObject = default(T)) { JSONElement childJSONElement = JSONUtils.FindChildWithAttributeValue(parentNode, kJSONFieldIdAttributeTag, id); return((T)FromJSONElement(typeof(T), childJSONElement, defualtObject)); }