Select to view content in your preferred language

FeatureSet.ToJson() produces invalid Json

2857
6
09-14-2011 01:10 PM
ChristianMorin
Emerging Contributor
We make use of the FeatureSet.ToJson() extensively and while investigation an issue with attribute containing quotes (") I discovered that the ToJson method does not escape quotes or any Json specific characters.

I've also discovered that the attributes all get transformed into strings independently of their types (integer and Boolean should not be converted to strings).

This means that if you take a feature set a perform FeatureSet.FromJson(fs.ToJson()) all your properly typed attributes are now strings.

I there a chance that this could be fixed for the next release (even 3.0 beta)?

Cheers,
Christian Morin.
0 Kudos
6 Replies
JenniferNery
Esri Regular Contributor
Thank you for your feedback. I was able to reproduce the bug. We'll try to fix it in future releases.
0 Kudos
ChristianMorin
Emerging Contributor
Thanks Jennifer for checking it out.

It turns out that there are more issues with the ToJson() method:

- The DisplayFieldName attribute does not get serialized
- The FieldAliases collection does not get serialized

These can also be verified by doing the FeatureSet.FromJson(fs.ToJson())

Cheers,
Christian.
0 Kudos
JenniferNery
Esri Regular Contributor
Actually to fix the bug you reported, we have included FeatureSet.Fields property. Setting this property will allow us to include Fields in the serialization and also help us detect the proper serialization per type (i.e. Date or Number field).
0 Kudos
ChristianMorin
Emerging Contributor
I can see how the Fields property would help to properly serialize the data type. But I'm not sure it would help with the new issue I've reported about the FeatureSet DisplayFieldName property that is not being serialized since this propert is on the FeatureSet itself and not in the features inside the FeatureSet.

I think the DisplayFieldName property was just missed, since properties such as GlobalIdFieldName and ObjectIdFieldName are serialized properly.

Cheers,
Christian.
0 Kudos
RyanCooney
Occasional Contributor
With the 2.3 release FeatureSets that do not contain geometries don't produce valid JSON if they have ObjectIdFieldName, GlobalIdFieldName, or SpatialReference values set.

FeatureSet fs = new FeatureSet();
fs.ObjectIdFieldName = "ID";
//fs.GlobalIdFieldName = "ID";
//fs.SpatialReference = new SpatialReference(4326);
fs.Features.Add(new ESRI.ArcGIS.Client.Graphic() { Attributes = { { "ID", "1" } } });
Console.WriteLine(fs.ToJson());


Produces this JSON:
{,"objectIdFieldName":"ID""features":[{"attributes":{"ID":"1"}}]}
0 Kudos
RyanCooney
Occasional Contributor
Also fails if the FeatureSet does not contain any features.

FeatureSet fs = new FeatureSet();
fs.ObjectIdFieldName = "ID";
Console.WriteLine(fs.ToJson());

{,"objectIdFieldName":"ID"}
0 Kudos