Rob Gates
Rob Gates

Reputation: 275

Issue with custom serialization using Newtonsoft JSON

To increase performance I tried to make a custom serializer to use with Newtonsoft JSON, however for some reason, the function stops randomly and the function calling the serializer ends as well. There are no exceptions, therefore I am unable to determine the cause. This is the function:

    public string customSerialize(List<EntityJSON> data)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        JsonTextWriter writer = new JsonTextWriter(sw);
        writer.WriteStartObject();
        writer.WriteStartArray(); // <-- Line 89
        foreach (EntityJSON json in data)
        {
            writer.WritePropertyName("type");
            writer.WriteValue(json.type);

            writer.WritePropertyName("name");
            writer.WriteValue(json.name);

            writer.WritePropertyName("position");
            writer.WriteStartArray();
            writer.WritePropertyName("x");
            writer.WriteValue(json.position.x);
            writer.WritePropertyName("z");
            writer.WriteValue(json.position.z);
            writer.WritePropertyName("rot");
            writer.WriteValue(json.position.rot);
            writer.WriteEndArray();

            writer.WritePropertyName("flags");
            writer.WriteValue(json.flags);
        }
        writer.WriteEndArray();
        writer.WriteEndObject();

        return sb.ToString();
    }

Update: Got the exception thanks to @Ayoub_B, this is the exception:Exception: Newtonsoft.Json.JsonWriterException: Token StartArray in state ObjectStart would result in an invalid JSON object. Path ''. ...

Sadly, I do not know what this means, I have marked the exception like in the code above (line 89).

Upvotes: 1

Views: 2689

Answers (1)

Ayoub_B
Ayoub_B

Reputation: 700

Are you by any chance calling this code under another thread? Maybe a Background Worker? If so then that might be the reason why the exception isn't thrown. Just wrap all the code inside a Try-Catch block and you'll be able to catch all the exceptions.

Update:

I think the exception is raised because you're putting an array inside a JSON object without a property name, If you did so, By inserting this line

writer.WritePropertyName("arrayKey");

before line 89, the previous exception will disappear, But you'll get a new one inside the loop, This new exception is caused because key-value pairs must be within an object {}, So you can't put them directly inside an Array.

At any rate, I fixed the code you posted and got the following result from it:

[
    {
        "type": "type_0",
        "name": "type_0",
        "position": {
            "x": "position.x_0",
            "z": "position.a_0",
            "rot": "position.rot_0"
        },
        "flags": "flags_0"
    }, 
    {
        "type": "type_1",
        "name": "type_1",
        "position": {
            "x": "position.x_1",
            "z": "position.a_1",
            "rot": "position.rot_1"
        },
        "flags": "flags_1"
    }
]

Code:

        writer.WriteStartArray(); // <-- Line 89
        for (int i = 0; i < 2; i++)
        {
            writer.WriteStartObject();
            writer.WritePropertyName("type");
            writer.WriteValue("type_" + i);

            writer.WritePropertyName("name");
            writer.WriteValue("type_" + i);

            writer.WritePropertyName("position");
            writer.WriteStartObject();

            writer.WritePropertyName("x");
            writer.WriteValue("position.x_" + i);
            writer.WritePropertyName("z");
            writer.WriteValue("position.a_" + i);
            writer.WritePropertyName("rot");
            writer.WriteValue("position.rot_" + i);
            writer.WriteEndObject();

            writer.WritePropertyName("flags");
            writer.WriteValue("flags_" + i);
            writer.WriteEndObject();
        }
        writer.WriteEndArray();

Upvotes: 1

Related Questions