private static void WriteValue(object obj, JsonWriter writer, bool writer_is_private, int depth) { if (depth > max_nesting_depth) { throw new JsonException( String.Format("Max allowed object depth reached while " + "trying to export from type {0}{1}", obj.GetType(), obj)); } //Null handler if (obj == null) { writer.Write(null); return; } Type obj_type = obj.GetType(); // See if there's a custom exporter for the object FIRST as in BEFORE ANYTHING ELSE!!! Love, Mark if (custom_exporters_table.ContainsKey(obj_type)) { ExporterFunc exporter = custom_exporters_table[obj_type]; exporter(obj, writer); return; } if (obj is IJsonWrapper) { if (writer_is_private) { writer.TextWriter.Write(((IJsonWrapper)obj).ToJson()); } else { ((IJsonWrapper)obj).ToJson(writer); } return; } /*** Check Base Types ***/ if (obj.GetType().ToString() == "System.MonoType") { writer.Write(obj.ToString()); return; } if (obj is String) { writer.Write((string)obj); return; } if (obj is Double) { writer.Write((double)obj); return; } if (obj is Int32) { writer.Write((int)obj); return; } if (obj is Boolean) { writer.Write((bool)obj); return; } if (obj is Int64) { writer.Write((long)obj); return; } if (obj is Array) { writer.WriteArrayStart(); foreach (object elem in (Array)obj) { WriteValue(elem, writer, writer_is_private, depth + 1); } writer.WriteArrayEnd(); return; } if (obj is IList) { Type valueType = obj.GetType().GetGenericArguments()[0]; writer.WriteArrayStart(); foreach (object elem in (IList)obj) { if (!valueType.IsAbstract) { WriteValue(elem, writer, writer_is_private, depth + 1); } else { writer.WriteObjectStart(); writer.WritePropertyName(elem.GetType().ToString()); WriteValue(elem, writer, writer_is_private, depth + 1); writer.WriteObjectEnd(); } } writer.WriteArrayEnd(); return; } if (obj is IDictionary) { writer.WriteObjectStart(); IDictionary dict = (IDictionary)obj; Type curType = obj.GetType(); bool isDict = typeof(IDictionary).IsAssignableFrom(curType); while (isDict) { isDict = typeof(IDictionary).IsAssignableFrom(curType.BaseType); if (isDict) { curType = curType.BaseType; } } Type valueType = curType.GetGenericArguments()[1]; foreach (DictionaryEntry entry in dict) { //This next line means we can't have anything but base types as keys. Love, Mark //writer.WritePropertyName (entry.Key.ToString()); if (IsBaseType(entry.Key)) { writer.WritePropertyName(entry.Key.ToString()); } else { JsonWriter newWriter = new JsonWriter(); JsonMapper.ToJson(entry.Key, newWriter); //string key = writer.JsonToString(newWriter.ToString()); string key = newWriter.ToString(); writer.WritePropertyName(key); } if (!valueType.IsAbstract) { WriteValue(entry.Value, writer, writer_is_private, depth + 1); } else { //Creates a second layer that stores the child type key of the object for decoding writer.WriteObjectStart(); if (entry.Value != null) { writer.WritePropertyName(entry.Value.GetType().ToString()); } else { writer.WritePropertyName("null"); } WriteValue(entry.Value, writer, writer_is_private, depth + 1); writer.WriteObjectEnd(); } } writer.WriteObjectEnd(); return; } /*Type obj_type = obj.GetType (); * * // See if there's a custom exporter for the object * if (custom_exporters_table.ContainsKey (obj_type)) { * ExporterFunc exporter = custom_exporters_table[obj_type]; * exporter (obj, writer); * * return; * }*/ // If not, maybe there's a base exporter if (base_exporters_table.ContainsKey(obj_type)) { ExporterFunc exporter = base_exporters_table[obj_type]; exporter(obj, writer); return; } // Last option, let's see if it's an enum if (obj is Enum) { Type e_type = Enum.GetUnderlyingType(obj_type); if (e_type == typeof(long) || e_type == typeof(uint) || e_type == typeof(ulong)) { writer.Write((ulong)obj); } else { writer.Write((int)obj); } return; } // Okay, so it looks like the input should be exported as an // object AddTypeProperties(obj_type); IList <PropertyMetadata> props = type_properties[obj_type]; writer.WriteObjectStart(); foreach (PropertyMetadata p_data in props) { bool skip = false; bool force = false; System.Object[] attrs = p_data.Info.GetCustomAttributes(false); if (attrs.Length > 0) { for (int j = 0; j < attrs.Length; j++) { if (attrs[j].GetType() == typeof(SkipSerialization)) { skip = true; break; } if (attrs[j].GetType() == typeof(ForceSerialization)) { force = true; break; } } } if (skip) { continue; } if (p_data.IsField) { FieldInfo f_info = ((FieldInfo)p_data.Info); if (f_info.GetValue(obj) == null && !force) { continue; } writer.WritePropertyName(p_data.Info.Name); if (f_info.FieldType.IsAbstract) { writer.WriteObjectStart(); writer.WritePropertyName(f_info.GetValue(obj).GetType().ToString()); depth++; } WriteValue(f_info.GetValue(obj), writer, writer_is_private, depth + 1); if (f_info.FieldType.IsAbstract) { writer.WriteObjectEnd(); } } else { PropertyInfo p_info = (PropertyInfo)p_data.Info; if (p_info.CanRead) { object propertyValue = GetPropertyValue(obj, p_info); if (propertyValue == null && !force) { continue; } writer.WritePropertyName(p_data.Info.Name); if (p_info.PropertyType.IsAbstract) { writer.WriteObjectStart(); //writer.WritePropertyName(p_info.GetValue(obj, null).GetType().ToString()); writer.WritePropertyName(propertyValue.GetType().ToString()); depth++; } //WriteValue (p_info.GetValue (obj, null), writer, writer_is_private, depth + 1); WriteValue(propertyValue, writer, writer_is_private, depth + 1); if (p_info.PropertyType.IsAbstract) { writer.WriteObjectEnd(); } } } } writer.WriteObjectEnd(); }
public void outNotes(){ Debug.Log("Start outnotes"); nowBGMName = AudioManager.Instance.getNowBGMName(); JsonMapper JsonMapper = new JsonMapper(); path = Application.persistentDataPath + "/Notes/" ; Debug.Log ("save path: "+path); // フォルダーがない場合は作成する if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //Notesのデータ jsontext = JsonMapper.ToJson(notes); File.WriteAllText(path + nowBGMName + ".json", jsontext); //Notesのjsonのデータ outjson = new OutJson(); outjson.set(nowBGMName,notes.Count); jsontext = JsonMapper.ToJson(outjson); File.WriteAllText(path + "Data-" +nowBGMName + ".json", jsontext); notes.Clear(); Debug.Log("Done outnotes"); }
public static void Serialize <T>(Stream stream, T o) { using (var s = new StreamWriter(stream)) s.Write(JsonMapper.ToJson(o)); }