public override string ObjectToJson(object obj) { if (obj == null) { return(null); } IDictionary dict = obj as IDictionary; EasyJsonWriter writer = new EasyJsonWriter(); JsonItemMetadata itemMetadata = _getItemMetadata(obj.GetType()); ObjectToJsonFunc keyFunc = _stringKeyToJsonName; if (itemMetadata.IndexType != typeof(string)) { keyFunc = base.ObjectToJson; } writer.WriteObjectStart(); foreach (DictionaryEntry entry in dict) { writer.WriteName(JsonUtil.Escape(keyFunc(entry.Key))); //writer.WriteJsonValue(base.ObjectToJson(entry.Value)); writer.WriteValue(entry.Value); } writer.WriteObjectEnd(); return(writer.ToString()); }
public virtual string ObjectToJson(object obj) { EasyJsonWriter writer = new EasyJsonWriter(); _depth = 0; _writeObject(obj, writer); return(writer.ToString()); }
void _writeObject(object obj, EasyJsonWriter writer) { if (obj == null) { writer.WriteJsonValue(null); return; } if (++_depth > MAX_DEPTH) { throw new EasyJsonException("Class nested level is beyond max depth, check whether there is a loop"); } Type t = obj.GetType(); EasyJsonController controller = GetController(t); if (controller != null) { writer.WriteJsonValue(controller.ObjectToJson(obj)); return; } writer.WriteObjectStart(); JsonClassMetadata metadata = _getMetadata(t); foreach (KeyValuePair <string, FieldInfo> kv in metadata.Fields) { _writeObject(kv.Key, kv.Value.GetValue(obj), writer); } foreach (KeyValuePair <string, PropertyInfo> kv in metadata.Properties) { MethodInfo getMethod = kv.Value.GetGetMethod(); if (getMethod == null) { throw new EasyJsonException("Fail to access getter of " + kv.Value.Name); } if (getMethod.GetParameters().Length > 0) { continue; } _writeObject(kv.Key, getMethod.Invoke(obj, null), writer); } if (metadata.IsDynamicType) { _writeObject(DYNAMIC_TYPE_NAME, t, writer); } writer.WriteObjectEnd(); writer.WriteJsonValue(""); }
public override string ObjectToJson(object obj) { if (obj == null) { return(null); } Array arr = obj as Array; int[] idx = new int[arr.Rank]; EasyJsonWriter writer = new EasyJsonWriter(); _writeArray(arr, idx, 0, writer); return(writer.ToString()); }
void _writeArray(Array arr, int[] startIdx, int dim, EasyJsonWriter writer) { writer.WriteArrayStart(); for (int i = 0; i < arr.GetLength(dim); ++i) { startIdx[dim] = i; if (dim == arr.Rank - 1) { //writer.WriteJsonValue(base.ObjectToJson(arr.GetValue(startIdx))); writer.WriteValue(arr.GetValue(startIdx)); } else { _writeArray(arr, startIdx, dim + 1, writer); writer.WriteJsonValue(""); } } writer.WriteArrayEnd(); }
public override string ObjectToJson(object obj) { if (obj == null) { return(null); } IList list = obj as IList; EasyJsonWriter writer = new EasyJsonWriter(); writer.WriteArrayStart(); foreach (object element in list) { //writer.WriteJsonValue(base.ObjectToJson(element)); writer.WriteValue(element); } writer.WriteArrayEnd(); return(writer.ToString()); }
public override string ToString() { if (Type == EasyJsonDataType.JsonObject) { EasyJsonWriter writer = new EasyJsonWriter(); writer.WriteObjectStart(); foreach (string name in Names) { writer.WriteName(name); writer.WriteJsonValue(this[name].ToString()); } writer.WriteObjectEnd(); return(writer.ToString()); } else if (Type == EasyJsonDataType.JsonArray) { EasyJsonWriter writer = new EasyJsonWriter(); writer.WriteArrayStart(); for (int i = 0; i < Count; ++i) { writer.WriteJsonValue(this[i].ToString()); } writer.WriteArrayEnd(); return(writer.ToString()); } else if (Type == EasyJsonDataType.JsonString) { return(JsonUtil.STRING_QUOTE + Data + JsonUtil.STRING_QUOTE); } else { return(Data); } }
void _writeObject(string name, object value, EasyJsonWriter writer) { writer.WriteName(name); _writeObject(value, writer); }