Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialized JSON is incorrect (Case #4) #18

Closed
amacal opened this issue Sep 20, 2014 · 5 comments
Closed

Serialized JSON is incorrect (Case #4) #18

amacal opened this issue Sep 20, 2014 · 5 comments

Comments

@amacal
Copy link

amacal commented Sep 20, 2014

When I serialized the following C# instance:

            return new Row
            {
                id = "5408a8fe87b96565151fd386",
                index = 17,
                guid = "76cbf5d2-7212-4586-9db6-a5c6bdac9c56",
                isActive = false,
                balance = "$3,143.46",
                picture = "http://placehold.it/32x32",
                age = 36,
                eyeColor = "green",
                name = "Craft Arnold",
                gender = "male",
                company = "ZOUNDS",
                email = "craftarnold@zounds.com",
                phone = "+1 (983) 535-2003",
                address = "431 Rewe Street, Norwood, American Samoa, 7252",
                about = "Laboris deserunt in culpa commodo anim est et officia enim adipisicing ea do. Occaecat consequat exercitation cupidatat commodo aliquip nulla officia tempor fugiat irure cupidatat. Proident sunt voluptate reprehenderit deserunt voluptate. Nulla et non consequat dolor. Dolore commodo in aliquip sint est non sit veniam duis reprehenderit culpa fugiat dolor sint. Magna sint labore mollit veniam elit mollit nulla minim aliqua.\r\n",
                registered = "2014-06-22T09:40:21 -02:00",
                latitude = 49.911697,
                longitude = -6.562665,
                tags = new string[] { "incididunt", "laborum", "proident", "quis", "do", "dolor", "reprehenderit" },
                friends = new[] 
                { 
                    new Row.Friend{ id = 0, name = "Maryann Rocha"},
                    new Row.Friend{ id = 1, name = "Stevens Luna"},
                    new Row.Friend{ id = 2, name = "Sanders Cantu"}
                },
                greeting = "Hello, Craft Arnold! You have 3 unread messages.",
                favoriteFruit = "banana"
            };

using the following class definition:

    public class Row
    {
        public string id { get; set; }
        public int index { get; set; }
        public string guid { get; set; }
        public bool isActive { get; set; }
        public string balance { get; set; }
        public string picture { get; set; }
        public int age { get; set; }
        public string eyeColor { get; set; }
        public string name { get; set; }
        public string gender { get; set; }
        public string company { get; set; }
        public string email { get; set; }
        public string phone { get; set; }
        public string address { get; set; }
        public string about { get; set; }
        public string registered { get; set; }
        public string greeting { get; set; }
        public double latitude { get; set; }
        public double longitude { get; set; }
        public string favoriteFruit { get; set; }

        public string[] tags { get; set; }
        public Friend[] friends { get; set; }

        public class Friend
        {
            public int id { get; set; }
            public string name { get; set; }
        }
    }

I receive the following JSON:

{"id":"5408a8fe87b96565151fd386","index":17,"guid":"76cbf5d2-7212-4586-9db6-a5c6bdac9c56","balance":"$3,143.46","picture":"http://placehold.it/32x32","age":36,"eyeColor":"green","name":"Craft Arnold","gender":"male","company":"ZOUNDS","email":"craftarnold@zounds.com","phone":"+1 (983) 535-2003","address":"431 Rewe Street, Norwood, American Samoa, 7252","about":"Laboris deserunt in culpa commodo anim est et officia enim adipisicing ea do. Occaecat consequat exercitation cupidatat commodo aliquip nulla officia tempor fugiat irure cupidatat. Proident sunt voluptate reprehenderit deserunt voluptate. Nulla et non consequat dolor. Dolore commodo in aliquip sint est non sit veniam duis reprehenderit culpa fugiat dolor sint. Magna sint labore mollit veniam elit mollit nulla minim aliqua.\r\n","registered":"2014-06-22T09:40:21 -02:00","greeting":"Hello, Craft Arnold! You have 3 unread messages.","latitude":49,911697,"longitude":-6,562665,"favoriteFruit":"banana","tags":["incididunt","laborum","proident","quis","do","dolor","reprehenderit"],"friends":[{,"name":"Maryann Rocha"},{"id":1,"name":"Stevens Luna"},{"id":2,"name":"Sanders Cantu"}]}

The fields latitude and longitude contains ',' instead of '.'.
The specification requires dot. I use Polish regional settings.

The friends array is corrupted. The first item doesn't contain id.

@rpgmaker
Copy link
Owner

It does not because I was checking for default value of type and
eliminating the field based on it. I can add a flag for eliminating
defaults and also fix the issue with leaving a comma in there for creating
default and also use a different formatter for float.

@amacal
Copy link
Author

amacal commented Sep 20, 2014

Eliminating default values could be 'questionable'. When you serializing the object currently you assume the client deserializing the object will use exactly the same class definition. In my case I could deserialize this missing (server-side int) id = 0 in client side as int? to null. You know, it can lead to problems. This optimization should be used when the implications are understood.

@rpgmaker
Copy link
Owner

That is why I will have a flag for determining when to eliminate primitive
type. It helps the payload size generally. So by default it will eliminate
them except you turn off the flag

@rpgmaker
Copy link
Owner

Test it again. I added a new flag to turn it off and on. The default is true, so you will have to set it to false or just compile the dll with false hard-coded. The issue with leading comma has been fixed too which causes invalid json.

@amacal
Copy link
Author

amacal commented Sep 21, 2014

It works, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants