Select to view content in your preferred language

Q: Howto serialize JSON string into Symbol in Silverlight API

891
3
10-27-2010 07:07 AM
StevenMorlock
Emerging Contributor
What's the best way in the Silverlight API to convert a basic json Symbol string (in this case a SimpleLineSymbol) to an object, without me needing to set the color and width manual.

"result" is a temporary object that i already created from the original json string.

Original JSON string:

Note that the SimpleLineSymbol only needs four properties: type, style, color and width. The other properties just need to be discarded; they are the cumulative collection of all the symbols properties.

{
"angle":null,
"backgroundColor":null,
"borderLineColor":null,
"color":[156,156,156,255],
"contentType":null,
"font":null,
"height":null,
"horizontalAlignment":null,
"imageData":null,
"kerning":null,
"outline":null,
"rightToLeft":null,
"size":null,
"style":"esriSLSSolid",
"type":"esriSLS",
"url":null,
"verticalAlignment":null,
"width":2,
"xoffset":null,
"xscale":null,
"yoffset":null,
"yscale":null
}


Current code to convert it:

                SimpleLineSymbol symbol = (SimpleLineSymbol)Json.ConvertToObject(json, typeof(SimpleLineSymbol));

                if (result.color != null && result.color.Count == 4)
                {
                    symbol.Color = new SolidColorBrush(Color.FromArgb(
                            (byte)(result.color[3] & 0xff), (byte)(result.color[0] & 0xff), (byte)(result.color[1] & 0xff), (byte)(result.color[2] & 0xff)));
                    symbol.Width = result.width;

                }


the current Json converter:

        public static object ConvertToObject(string json, Type type)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                byte[] bytes = Encoding.UTF8.GetBytes(json);
                memoryStream.Write(bytes, 0, bytes.Length);
                DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(type);

                return dataContractJsonSerializer.ReadObject(memoryStream);
            }
        }


Thanks for your feedback.
0 Kudos
3 Replies
JenniferNery
Esri Regular Contributor
You can update your color and width conversion this way.

symbol.Color = Color.FromArgb(Convert.ToByte(result.color[3]), Convert.ToByte(result.color[0]), Convert.ToByte(result.color[1]), Convert.ToByte(result.color[2]));
symbol.Width =  Convert.ToDouble(pts, CultureInfo.InvariantCulture) * 96 / 72; // to convert from points to pixel units
0 Kudos
StevenMorlock
Emerging Contributor
You can update your color and width conversion this way.

symbol.Color = Color.FromArgb(Convert.ToByte(result.color[3]), Convert.ToByte(result.color[0]), Convert.ToByte(result.color[1]), Convert.ToByte(result.color[2]));
symbol.Width =  Convert.ToDouble(pts, CultureInfo.InvariantCulture) * 96 / 72; // to convert from points to pixel units


Hi Jennifer,

thanks for your reply.
I'm not exactly concerned in setting the values of the symbol object. I already got that working (as you can see in my code). I'm interested in converting the original JSON (REST) result into a Symbol were these values are already populated.

It seems akward and strange to serialize my object and then have to set the color and width again, even when that data is already available in the JSON resultset.

I want to convert
{
"color":[156,156,156,255],
"style":"esriSLSSolid",
"type":"esriSLS",
"width":2,
}


To a SimpleLineSymbol using a Serializer without doing some stuff on the fly to set the Color and Width. The information is already there so you would expect the serializer to do this for me.

So the question is, how can this be achieved?! Is there some method/class available in the ESRI Toolkit that provides this?

Thank you for your time
0 Kudos
StevenMorlock
Emerging Contributor
So some further investigation shows that the SimpleLineSymbol doesn't have any DataContract and DataMember attributes set. This is why the serialization fails.

When can we expect this to be implemented? I would expect this to be already available for converting JSON to objects. Now you have to do a lot of manual work.
0 Kudos