public override void Serialize(JsonHelperWriter json, object obj)
    {
        DictionaryEntry entry = (DictionaryEntry)obj;

        json.WriteProperty("key", entry.Key);
        json.WriteProperty("value", entry.Value);
    }
    public override void Serialize(JsonHelperWriter json, object obj)
    {
        Vector2 v = (Vector2)obj;

        json.WriteProperty("x", v.x);
        json.WriteProperty("y", v.y);
    }
Exemple #3
0
    public void SerializeComponentData(JsonHelperWriter json, GameObject go, bool property = true)
    {
        Component[] components = go.GetComponents <Component>();

        Transform transform = go.transform;
        int       children  = transform.childCount;

        if (property)
        {
            json.WritePropertyName("componentData");
        }
        json.WriteStartArray();

        json.WriteValue(components.Length);
        for (int i = 0; i < components.Length; i++)
        {
            json.Write(components[i]);
        }

        json.WriteStartArray();
        for (int i = 0; i < children; i++)
        {
            GameObject child = transform.GetChild(i).gameObject;
            SerializeComponentData(json, child, false);
        }
        json.WriteEndArray();

        json.WriteEndArray();
    }
    public static void WriteJSON(this object obj, string path)
    {
        if (obj == null)
        {
            return;
        }
        if (obj is JToken)
        {
            File.Delete(path);
            File.WriteAllText(path, obj.ToString());
            return;
        }

        Type type = obj.GetType();

        if (obj is Enum || obj is string || obj is byte[] || type.IsPrimitive)
        {
            JToken.FromObject(obj).WriteJSON(path);
            return;
        }

        using (JsonHelperWriter json = OpenWriteJSON(path)) {
            json.Write(obj);
        }
    }
 public static void WriteAll(this JsonHelperWriter json, JSONRule rule, object obj, MemberInfo[] infos)
 {
     for (int i = 0; i < infos.Length; i++)
     {
         rule.Serialize(json, obj, infos[i]);
     }
 }
Exemple #6
0
 public override void Serialize(JsonHelperWriter json, object obj)
 {
     try {
         json.WriteProperty("text", ((TextAsset)obj).text);
     } catch {
         json.WriteProperty("text", null);
     }
 }
Exemple #7
0
    public void SerializeMain(JsonHelperWriter json, GameObject go)
    {
        json.WriteProperty("name", go.name);

        json.WriteProperty("tag", go.tag);
        json.WriteProperty("layer", go.layer);
        json.WriteProperty("activeSelf", go.activeSelf);
    }
Exemple #8
0
    protected override void Serialize_(JsonHelperWriter json, object obj)
    {
        Transform t = (Transform)obj;

        // TODO all the (2) various types of Transforms
        json.WriteProperty("position", t.position);
        json.WriteProperty("rotation", t.rotation);
        json.WriteProperty("localScale", t.localScale);
    }
    public override void Serialize(JsonHelperWriter json, object obj)
    {
        Quaternion q = (Quaternion)obj;

        json.WriteProperty("x", q.x);
        json.WriteProperty("y", q.y);
        json.WriteProperty("z", q.z);
        json.WriteProperty("w", q.w);
    }
Exemple #10
0
    public override void Serialize(JsonHelperWriter json, object obj)
    {
        GameObject go = (GameObject)obj;

        SerializeMain(json, go);
        SerializeComponentTypes(json, go);
        SerializeHierarchy(json, go);

        SerializeComponentData(json, go);
    }
Exemple #11
0
 public static void WriteMetaArrayData(this JsonHelperWriter json, string type, int size = -1)
 {
     json.WriteStartMetadata(META.ARRAYTYPE);
     json.WriteProperty(META.TYPE_FULLNAME, type);
     if (type == META.ARRAYTYPE_ARRAY)
     {
         json.WriteProperty(META.ARRAYTYPE_ARRAY_SIZE, size);
     }
     json.WriteEndMetadata();
 }
Exemple #12
0
    public static JsonHelperWriter OpenWriteJSON(string path)
    {
        File.Delete(path);
        Stream           stream = File.OpenWrite(path);
        StreamWriter     text   = new StreamWriter(stream);
        JsonHelperWriter json   = new JsonHelperWriter(text);

        json.RelativeDir = path.Substring(0, path.Length - 5);
        json.Formatting  = Formatting.Indented;
        return(json);
    }
Exemple #13
0
    public static void WriteMetaExternal(this JsonHelperWriter json, string path, string @in = META.EXTERNAL_IN_RESOURCES)
    {
        json.WriteStartMetadata(META.EXTERNAL);

        if (@in != META.EXTERNAL_IN_RESOURCES)
        {
            json.WriteProperty(META.EXTERNAL_IN, @in);
        }
        json.WriteProperty(META.EXTERNAL_PATH, path);

        json.WriteEndMetadata();
    }
Exemple #14
0
    public void SerializeComponentTypes(JsonHelperWriter json, GameObject go)
    {
        Component[] components = go.GetComponents <Component>();

        json.WritePropertyName("componentTypes");
        json.WriteStartArray();
        for (int i = 0; i < components.Length; i++)
        {
            json.Write(components[i].GetType());
        }
        json.WriteEndArray();
    }
Exemple #15
0
    public override void Serialize(JsonHelperWriter json, object obj)
    {
        Component c = (Component)obj;

        if (json.At(-2) is GameObject)
        {
            json.WriteProperty(JSONHelper.META.TYPE, JSONHelper.META.COMPONENTTYPE_DEFINITION);
            Serialize_(json, obj);
        }
        else
        {
            json.WriteProperty(JSONHelper.META.TYPE, JSONHelper.META.COMPONENTTYPE_REFERENCE);
            json.WriteProperty("name", c.gameObject.name);
            json.WriteProperty("path", c.transform.GetPath());
        }
    }
Exemple #16
0
    public void SerializeHierarchy(JsonHelperWriter json, GameObject go)
    {
        Transform transform = go.transform;
        int       children  = transform.childCount;

        json.WritePropertyName("hierarchy");
        json.WriteStartArray();
        for (int i = 0; i < children; i++)
        {
            GameObject child = transform.GetChild(i).gameObject;
            json.WriteStartObject();
            SerializeMain(json, child);
            SerializeComponentTypes(json, child);
            SerializeHierarchy(json, child);
            json.WriteEndObject();
        }
        json.WriteEndArray();
    }
Exemple #17
0
    public virtual void Serialize(JsonHelperWriter json, object obj)
    {
        if (obj is UnityEngine.Object && !(obj is Component))
        {
            json.WriteProperty("name", ((UnityEngine.Object)obj).name);
        }

        if (ForceSerializeProperties)
        {
            for (int i = 0; i < _Properties.Length; i++)
            {
                Serialize(json, obj, _Properties[i]);
            }
        }

        for (int i = 0; i < _Fields.Length; i++)
        {
            Serialize(json, obj, _Fields[i]);
        }
    }
Exemple #18
0
    public static bool TryWriteMetaReference(this JsonHelperWriter json, object obj, bool register = false)
    {
        int id = json.GetReferenceID(obj);

        if (id != META.REF_NONE)
        {
            json.WriteStartMetadata(META.REF);

            json.WriteProperty(META.REF_ID, id);
            json.WriteProperty(META.REF_TYPE, json.GetReferenceType(id, obj));

            json.WriteEndMetadata();
            return(true);
        }

        if (register)
        {
            json.RegisterReference(obj);
        }
        return(false);
    }
Exemple #19
0
    private static void WriteMetaType_(this JsonHelperWriter json, Type type, string metaType)
    {
        json.WriteStartMetadata(metaType);

        json.WriteProperty(META.TYPE_FULLNAME, type.FullName);

        /*string ns = type.Namespace;
         * if (ns != null) {
         *  json.WritePropertyName(META.TYPE_SPLIT);
         *  json.WriteStartArray();
         *  json.Write(ns);
         *  json.Write(type.Name);
         *  json.WriteEndArray();
         * }*/
        Type[] genparams = type.GetGenericArguments();
        if (genparams.Length != 0)
        {
            json.WriteProperty(META.TYPE_GENPARAMS, genparams);
        }

        json.WriteEndMetadata();
    }
Exemple #20
0
 public static void Write(this JsonHelperWriter json, JSONRule rule, object obj, MemberInfo info)
 {
     rule.Serialize(json, obj, info);
 }
Exemple #21
0
 public static void WriteProperty(this JsonHelperWriter json, string name, object obj)
 {
     json.WritePropertyName(name);
     json.Write(obj);
 }
Exemple #22
0
 public static void WriteMetaType(this JsonHelperWriter json, Type type)
 {
     json.WriteMetaType_(type, META.TYPE);
 }
Exemple #23
0
 public static void WriteMetaObjectType(this JsonHelperWriter json, object obj)
 {
     json.WriteMetaType_(obj.GetType(), META.OBJTYPE);
 }
Exemple #24
0
    public static void Write(this JsonHelperWriter json, object obj)
    {
        if (obj == null)
        {
            json.WriteNull();
            return;
        }
        if (obj is JToken)
        {
            json.WriteRawValue(obj.ToString());
            return;
        }

        Type type = obj.GetType();

        if (obj is Enum || obj is string || obj is byte[] || type.IsPrimitive)
        {
            json.WriteValue(obj);
            return;
        }

        if (obj is Type)
        {
            json.WriteMetaType((Type)obj);
            return;
        }

        if (json.TryWriteMetaReference(obj, true))
        {
            return;
        }

        json.Push(obj);

        JSONRule rule = type.GetJSONRule();

        if (rule.GetType() == t_JSONRule)
        {
            if (obj is IList)
            {
                IList list = (IList)obj;
                json.WriteStartArray();
                if (type.IsArray)
                {
                    json.WriteMetaArrayData(META.ARRAYTYPE_ARRAY, list.Count);
                }
                else
                {
                    json.WriteMetaArrayData(META.ARRAYTYPE_LIST);
                }

                foreach (object o in list)
                {
                    json.Write(o);
                }
                json.WriteEndArray();
                json.Pop();
                return;
            }

            if (obj is IDictionary)
            {
                IDictionary dict = (IDictionary)obj;
                json.WriteStartArray();
                json.WriteMetaArrayData(META.ARRAYTYPE_MAP);
                foreach (DictionaryEntry e in dict)
                {
                    json.Write(e);
                }
                json.WriteEndArray();
                json.Pop();
                return;
            }
        }

        UnityEngine.Object so = (UnityEngine.Object)(
            ((object)(obj as GameObject)) ??
            ((object)(obj as ScriptableObject)) ??
            ((object)(obj as Component))
            );
        string name = so?.name;

        if (json.RootWritten && (json.DumpRelatively || SharedDir != null) && !string.IsNullOrEmpty(name) && !(obj is Transform))
        {
            if (SharedDir == null && json.DumpRelatively)
            {
                Directory.CreateDirectory(json.RelativeDir);
                string dumppath = Path.Combine(json.RelativeDir, name + ".json");
                if (!File.Exists(dumppath))
                {
                    using (JsonHelperWriter ext = OpenWriteJSON(dumppath)) {
                        ext.AddPath(json);
                        ext.RelativeDir = Path.Combine(json.RelativeDir, name);
                        ext.Write(obj);
                    }
                }
                json.WriteMetaExternal(name, META.EXTERNAL_IN_RELATIVE);
            }
            else if (SharedDir != null)
            {
                string path;
                if (_DumpObjPathMap.TryGetValue(so, out path))
                {
                    json.WriteMetaExternal(path, META.EXTERNAL_IN_SHARED);
                    json.Pop();
                    return;
                }
                path = type.Name + "s/" + name;

                int id;
                if (!_DumpNameIdMap.TryGetValue(path, out id))
                {
                    id = -1;
                }
                _DumpNameIdMap[name] = ++id;

                if (id != 0)
                {
                    path += "." + id;
                }
                _DumpObjPathMap[so] = path;

                string dumppath = Path.Combine(SharedDir, path.Replace('/', Path.DirectorySeparatorChar) + ".json");
                Directory.GetParent(dumppath).Create();
                if (!File.Exists(dumppath))
                {
                    using (JsonHelperWriter ext = OpenWriteJSON(dumppath)) {
                        ext.AddPath(json);
                        ext.Write(obj);
                    }
                }
                json.WriteMetaExternal(path, META.EXTERNAL_IN_SHARED);
            }
            json.Pop();
            return;
        }

        json.RootWritten = true;
        json.WriteStartObject();
        rule.WriteMetaHeader(json, obj);
        _OnBeforeSerialize(obj);
        rule.Serialize(json, obj);
        json.WriteEndObject();
        json.Pop();
    }
Exemple #25
0
 public void AddPath(JsonHelperWriter json)
 {
     _ObjPath.AddRange(json._ObjPath);
 }
Exemple #26
0
 protected virtual void Serialize_(JsonHelperWriter json, object obj)
 {
     base.Serialize(json, obj);
 }
 public override void WriteMetaHeader(JsonHelperWriter json, object obj)
 {
 }
Exemple #28
0
 public static void WriteStartMetadata(this JsonHelperWriter json, string metaType)
 {
     json.WriteStartObject();
     json.WriteProperty(META.MARKER, metaType);
 }
 public override void WriteMetaHeader(JsonHelperWriter json, object obj)
 {
     // json.WriteProperty(JSONHelper.META.PROP, JSONHelper.META.VALUETYPE);
 }
Exemple #30
0
 public static void WriteEndMetadata(this JsonHelperWriter json)
 {
     json.WriteEndObject();
 }