Search Unity

Assets [WIP] UON : The Unity JSON

Discussion in 'Works In Progress - Archive' started by Mikilo, Jul 9, 2017.

  1. Mikilo

    Mikilo

    Joined:
    Jan 29, 2013
    Posts:
    694
    Hello folks!

    I wish to share a tool recently developed which allow me to convert any object into UON text and any UON text into object.

    UON is an equivalent of JSON, but it follows its own syntax and rules.
    UON handles almost any types, generics, arrays, lists, dictionaries, classes, structs, interfaces and Unity Object!

    Usage: Use the class UON which is in the namespace NGTools.UON.
    There is 2 methods in it:
    Code (CSharp):
    1. string   ToUON(object o);
    2. // Converts any object into UON.
    3.  
    4. object   FromUON(string raw, object instance = null);
    5. // Converts any UON into object.
    6. // If you provide the instance, UON will fill it with the given input.

    This tool is still experimental.

    If you stumble upon any issues or have any suggestions, please let me know.
     

    Attached Files:

    • UON.rar
      File size:
      15.8 KB
      Views:
      388
    Last edited: Jul 14, 2017
  2. Mikilo

    Mikilo

    Joined:
    Jan 29, 2013
    Posts:
    694
    Hello people!

    As you might have noticed, ToUON and FromUON do not ask you a Type, because it is embedded directly into the format.

    UON does not ask for any attribute or contract or any constraint.
    It serializes every public and private fields.

    But it provides the interface IUONDeserialization:
    Code (CSharp):
    1. public interface IUONDeserialization
    2. {
    3.    void    OnDeserialized(DeserializationData data);
    4. }
    Which is invoked on your object after it has been deserialized if needs be.


    Here are several cases that might interest you:

    Code (CSharp):
    1. // Note that you can not directly serialize a Unity Object, because UON will save its fields instead of the Unity reference.
    2. Debug.Log(UON.ToUON(new Object[] { AssetDatabase.LoadAssetAtPath<Object>("Assets/Plugins/NGTools/UON/DeserializationData.cs") }));
    3. // [["UnityEngine.Object[],UnityEngine","UnityEditor.MonoScript,UnityEditor"],[(1)"Assets/Plugins/NGTools/UON/DeserializationData.cs"]]
    4.  
    5. Debug.Log(UON.ToUON(null));
    6. // []
    7.  
    8. Debug.Log(UON.ToUON(1));
    9. // [["System.Int32"],1]
    10.  
    11. Debug.Log(UON.ToUON(12345));
    12. // [["System.Int32"],12345]
    13.  
    14. Debug.Log(UON.ToUON(-12345));
    15. // [["System.Int32"],-12345]
    16.  
    17. Debug.Log(UON.ToUON(1.234F));
    18. // [["System.Single"],1.234]
    19.  
    20. Debug.Log(UON.ToUON(0F));
    21. // [["System.Single"],0]
    22.  
    23. Debug.Log(UON.ToUON(-1.234F));
    24. // [["System.Single"],-1.234]
    25.  
    26. Debug.Log(UON.ToUON(true));
    27. // [["System.Boolean"],T]
    28.  
    29. Debug.Log(UON.ToUON(false));
    30. // [["System.Boolean"],F]
    31.  
    32. Debug.Log(UON.ToUON(string.Empty));
    33. // [["System.String"],""]
    34.  
    35. Debug.Log(UON.ToUON("Abcdefg"));
    36. // [["System.String"],"Abcdefg"]
    37.  
    38. Debug.Log(UON.ToUON(new int[] { 1, 0, -1 }));
    39. // [["System.Int32[]","System.Int32"],[(1)1,0,-1]]
    40.  
    41. // NOTE: Only byte and sbyte are written using hexadecimal!
    42. Debug.Log(UON.ToUON(new byte[] { 1, 0, 255 }));
    43. // [["System.Byte[]","System.Byte"],[(1)1,0,ff]]
    44.  
    45. Debug.Log(UON.ToUON(new sbyte[] { -123, 1, 0, 124 }));
    46. // [["System.SByte[]","System.SByte"],[(1)85,1,0,7c]]
    47.  
    48. Debug.Log(UON.ToUON(new float[] { 1.234567F, 0.1234567F, -1.234567F }));
    49. // [["System.Single[]","System.Single"],[(1)1.234567,0.1234567,-1.234567]]
    50.  
    51. Debug.Log(UON.ToUON(new string[] { "aa", "bb", "cc", string.Empty, null }));
    52. // [["System.String[]","System.String"],[(1)"aa","bb","cc","",NULL]]
    53.  
    54. Debug.Log(UON.ToUON(new string[] { }));
    55. // [["System.String[]"],[]]
    56.  
    57. Debug.Log(UON.ToUON(new List<int> { 1, 0, -1 });
    58. // [["System.Collections.Generic.List`1[[System.Int32]]","System.Int32"],[(1)1,0,-1]]
    59.  
    60. Debug.Log(UON.ToUON(new List<string> { "aa", "bb", "cc", string.Empty, null }));
    61. // [["System.Collections.Generic.List`1[[System.String]]","System.String"],[(1)"aa","bb","cc","",NULL]]
    62.  
    63. Debug.Log(UON.ToUON(new Dictionary<int, byte>()));
    64. // [["System.Collections.Generic.Dictionary`2[[System.Int32],[System.Byte]]","System.Int32[]","System.Int32","System.Collections.Generic.Link[]","System.Collections.Generic.Link","System.Byte[]","System.Byte","System.Collections.Generic.GenericEqualityComparer`1[[System.Int32]]"],{"table":(1)[(2)0,0,0,0,0,0,0,0,0,0,0,0],"linkSlots":(3)[(4){"HashCode":(2)0,"Next":0},(4){"HashCode":(2)0,"Next":0},(4){"HashCode":(2)0,"Next":0},(4){"HashCode":(2)0,"Next":0},(4){"HashCode":(2)0,"Next":0},(4){"HashCode":(2)0,"Next":0},(4){"HashCode":(2)0,"Next":0},(4){"HashCode":(2)0,"Next":0},(4){"HashCode":(2)0,"Next":0},(4){"HashCode":(2)0,"Next":0},(4){"HashCode":(2)0,"Next":0},(4){"HashCode":(2)0,"Next":0}],"keySlots":(1)[(2)0,0,0,0,0,0,0,0,0,0,0,0],"valueSlots":(5)[(6)00,00,00,00,00,00,00,00,00,00,00,00],"touchedSlots":(2)0,"emptySlot":-1,"count":0,"threshold":10,"hcp":(7){},"serialization_info":NULL,"generation":(2)0}]
    65.  
    66. Debug.Log(UON.ToUON(new List<Action<int, Action<string, Dictionary<int, LocalVariableInfo>>>>()));
    67. // [["System.Collections.Generic.List`1[[System.Action`2[[System.Int32],[System.Action`2[[System.String],[System.Collections.Generic.Dictionary`2[[System.Int32],[System.Reflection.LocalVariableInfo]]]],System.Core]],System.Core]]"],[]]
     
    Last edited: Jul 14, 2017
  3. Gogeric

    Gogeric

    Joined:
    Apr 5, 2013
    Posts:
    7
    Seems kind of interesting, but I suppose you use reflection internally?
     
    Mikilo likes this.
  4. Mikilo

    Mikilo

    Joined:
    Jan 29, 2013
    Posts:
    694
    I do.

    I don't know any technique that don't use Reflection when dealing with any given input.
    If it is not the user who gives the data, then I guess Reflection is my only friend.

    You have all the sources in the given archive if you want.
     
  5. ExDeaDguY

    ExDeaDguY

    Joined:
    Aug 25, 2009
    Posts:
    504
    Not to bring this down or anything.. But why? Why not just stick with JSON? Don't fix it if it ain't broken. Not to mention JSON is widely used which makes support much easier to find.
     
    Mikilo likes this.
  6. Mikilo

    Mikilo

    Joined:
    Jan 29, 2013
    Posts:
    694
    You are right.

    I completely forgot to add an example using Unity Object...

    UON can handle Unity Object and ScriptableObject as well.

    Of course, the Object must be known by the AssetDatabase.
    And I am thinking about making it works while playing.


    This tool is especially useful when saving/loading data in the Editor.
    So... the target might be tool programmers.
     
  7. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    Why not make it JSON compatible? It should be more easy if we need to handle the data outside Unity.
     
    Mikilo likes this.
  8. Mikilo

    Mikilo

    Joined:
    Jan 29, 2013
    Posts:
    694
    I had the JSON compatibility in mind when developing UON.
    But during the development I removed that compatibility.

    For a simple reason, for now I am the only one using it.
    And I wanted it to have the smallest size footprint, as much as possible.
    Adding the compatibility would have added too much overhead for nothing.

    But if it is demanded, I will reimplement a strict JSON mode with pleasure.
     
  9. Mikilo

    Mikilo

    Joined:
    Jan 29, 2013
    Posts:
    694
    The code has been updated. No more compile errors... Sorry for this huge mistake, that was really stupid...
     
  10. AGregori

    AGregori

    Joined:
    Dec 11, 2014
    Posts:
    527
    Also, please try to make a game build with the new UON-based NGTools (0.41?). Showstopping compile error, please update.
     
    Mikilo likes this.
  11. Mikilo

    Mikilo

    Joined:
    Jan 29, 2013
    Posts:
    694
    Working on it...
     
  12. Mikilo

    Mikilo

    Joined:
    Jan 29, 2013
    Posts:
    694
    I just sent you the fixed version.
     
    AGregori likes this.