Bug: Serialization of Attribute values

2375
5
01-09-2014 02:28 AM
Labels (1)
BKuiper
Occasional Contributor III
Hi,

I found a bug in ArcGIS Runtime for WPF version: 10.1.1.0 when using ToJson() to serialize a GraphicsLayer. It seems that attribute values are not serialized correctly as they lose their original datatype and are converted into strings.

the error is in the following line
                        jw.WriteProperty(pair.Key, (pair.Value != null) ? string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { pair.Value }) : null);

within
        public static void WriteAttributes(ArcGISJsonWriter jw, IDictionary<string, object> attributes, bool onlyDirty, Dictionary<string, object> dirtyList, List<string> fields, string objectIdField)

Because of the string.format() call the value of the attribute, whether it is a boolean, double or string, is converted into a string and then stored as
key: "value"

in the json, instead of (with a double)
key: value

This bug is pretty inconvenient for us as it affects our application and thus our users.

Is this fixed in the latest release of ArcGIS Runtime ? Sadly enough we can't yet upgrade to VS2012 or VS2013 and are still using VS2010.

Can you confirm the bug and perhaps give advise on how to proceed to get this fixed for our installation that would work on VS2010.

Thanks!
0 Kudos
5 Replies
DavidLednik
Occasional Contributor II
Hi Bjorn,

From the code provided it's not really clear how you serialize the layer.
Can you show bigger block of code or even better attach a small reproducible sample app?

regards,
David
0 Kudos
BKuiper
Occasional Contributor III
Hi David,

I'm calling ToJson on FeatureSet. I get the GraphicsLayer, group by geometry and serialize the group one by one by creating a new FeatureSet object and calling ToJson(true).

So the problem is that the attributes on the FeatureSet are stored as key,value <string, string> instead of <string, object>, due to the call to string.format as explained in my original post.

                    GraphicsLayer gl = layer as GraphicsLayer;
                    if (gl != null)
                    {
                        var graphicsByGeometry = gl.Graphics.GroupBy(x => x.Geometry.GetType());
                        foreach (var group in graphicsByGeometry)
                        {
                            List<Graphic> graphics = group.ToList();
                            FeatureSet fs = new FeatureSet(graphics);
                            jsonfeaturesets.JsonFeatures.Add(fs.ToJson(true));
                        }
                    }
0 Kudos
BKuiper
Occasional Contributor III
It seems that the code in 10.2 is still broken

                    if (fields == null)
                    {
                        jw.WriteProperty(pair.Key, (pair.Value != null) ? string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { pair.Value }) : null);
                    }


in

        public static void WriteAttributes(ArcGISJsonWriter jw, IDictionary<string, object> attributes, bool onlyDirty, Dictionary<string, object> dirtyList, List<string> fields, string objectIdField)


of class ESRI.ArcGIS.Client.Tasks.Utils.JSON.ArcGISJsonWriter

It should not use string.format to write the result as attributes are of dictionary type <string, object> not <string, string> and in this case, will lose its type definition.
0 Kudos
BKuiper
Occasional Contributor III
It seems that setting the Fields collection on the FeatureSet object (by looping through the attributes and creating them) mitigates this issue.
0 Kudos
dotMorten_esri
Esri Notable Contributor

Yes it is required to set the Fields property to ensure proper type serialization, or the fallback will be strings.

0 Kudos