Search code examples
c#serializationwindows-phone-7datacontractserializer

Using DataContractJsonSerialiser to parse a json file


I have json file that I have exported using TexturePacker, and it produces this format.

{"frames": {

    "But_01_Highlight.png":
    {
        "frame": {"x":0,"y":0,"w":280,"h":41},
        "rotated": false,
        "trimmed": true,
        "spriteSourceSize": {"x":7,"y":8,"w":280,"h":41},
        "sourceSize": {"w":294,"h":57}
    },

I was following a tutorial where you could just turn it into an array but that's not available on windows phone 7. It feels like I have the reverse engineer every format rather than just read it as is parsed.

How would I create an object with a datacontract to load this format?

My question is also similar to the following question https://stackoverflow.com/questions/3769322/datacontractjsonserializer-with-arbitrary-key-names which has no answer

@Andreas Löw if you could export to a format like so it would be great.

{"frames":[ 
    {
        "filename": "But_01_Highlight.png",
        "frame": {"x":0,"y":0,"w":280,"h":41},
        "rotated": false,
        "trimmed": true,
        "spriteSourceSize": {"x":7,"y":8,"w":280,"h":41},
        "sourceSize": {"w":294,"h":57}
    },
    ...
    ]

Solution

  • The tool at http://carlosfigueira.me/JsonUtilities/JsonToContract.htm (described in the blog post http://blogs.msdn.com/b/carlosfigueira/archive/2011/01/11/inferring-schemas-for-json.aspx) can be used to create an object graph which can be used to deserialize that JSON using the DataContractJsonSerializer. This is the output of the tool (I had to change the name of the class "But_01_Highlight.png" to "But_01_Highlight_png" because of a bug in the tool).

    Also, this works given that all the JSON data follows the same "schema". If this is not the case, then the DataContractJsonSerializer is not the best option for that. For WP7, you can use some JSON library such as the classes on the System.Json namespace (you need to add a reference to the System.Json.dll from the Silverlight 3.0 SDK)

    [System.Runtime.Serialization.DataContractAttribute()]
    public partial class FrameClass
    {
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public int x;
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public int y;
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public int w;
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public int h;
    }
    
    [System.Runtime.Serialization.DataContractAttribute()]
    public partial class SourceSizeClass
    {
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public int w;
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public int h;
    }
    
    [System.Runtime.Serialization.DataContractAttribute()]
    public partial class But_01_Highlight_pngClass
    {
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public FrameClass frame;
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public bool rotated;
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public bool trimmed;
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public FrameClass spriteSourceSize;
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public SourceSizeClass sourceSize;
    }
    
    [System.Runtime.Serialization.DataContractAttribute()]
    public partial class FramesClass
    {
    
        [System.Runtime.Serialization.DataMemberAttribute(Name = "But_01_Highlight.png")]
        public But_01_Highlight_pngClass But_01_Highlight_png;
    }
    
    [System.Runtime.Serialization.DataContractAttribute()]
    public partial class RootClass
    {
    
        [System.Runtime.Serialization.DataMemberAttribute()]
        public FramesClass frames;
    }