1

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}
    },
    ...
    ]

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

the .png obviously would have to fit into a string there it's a value and not a class name in this instance. but thanks for the link to the tool.
Yes, I forgot to update the "Name" Property in the data contract attribute. It should work now.
i don't this will work, what if the .png files list is generated dynamically by a service. "But_01_Highlight.png" should be dynamically loaded by the serializer.
As I mentioned in the answer, the DataContractJsonSerializer (DCJS) works if the JSON complies with a fixed schema (i.e., the names of the object members are constant). If this is not the case, then the DCJS really isn't the solution for your problem, you'll need to use some tool / framework / library which will let you load the JSON in a DOM-like structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.