JSON issues

Is bool supported in JSON serializer?

i get this error:

#### Exception System.Exception - 0x00000000 (3) ####
#### Message: unsupported type for Marshall
#### GHIElectronics.TinyCLR.Data.Json.SerializationUtilities::Marshall [IP: 0410] ####
#### GHIElectronics.TinyCLR.Data.Json.JValue::ToBson [IP: 0012] ####
#### GHIElectronics.TinyCLR.Data.Json.JProperty::ToBson [IP: 000f] ####
#### GHIElectronics.TinyCLR.Data.Json.JToken::ToBson [IP: 001e] ####
#### GHIElectronics.TinyCLR.Data.Json.JObject::ToBson [IP: 0031] ####
#### GHIElectronics.TinyCLR.Data.Json.JToken::ToBson [IP: 001e] ####
#### GHIElectronics.TinyCLR.Data.Json.JToken::ToBson [IP: 0019] ####
#### VepelDevice.Connect::Write [IP: 000a] ####
#### VepelDevice.Program::Connect_DataReceived [IP: 0071] ####
#### VepelDevice.Connect::UsbClient_DataReceived [IP: 000a] ####
#### GHIElectronics.TinyCLR.Devices.UsbClient.UsbClientController::OnDataReceived [IP: 000e] ####
#### GHIElectronics.TinyCLR.Devices.UsbClient.Provider.UsbClientControllerApiWrapper::<.ctor>b__8_0 [IP: 001c] ####
 Uncaught exception 

Also i tried JSON from WebApi app and it can deserialize correctly, now im trying out Winforms app over serial port with newtonsoft json and its not working.
What are my options for serialization from winforms to tinyclr?

Why is .ToString prettifying JSON?
Is .ToBson also doing this?
Im gonna try to convert that string to byte array and send it that way over serial port.

I do not think it is but we can investigate. This library is provided by the expert @mcalsyn so maybe he can provide some direction.

I’ll take a look.

I flagged this for deletion, it works now for me…

Ok - good to hear - I have been working today on a fix for issue 540 that I will be PR’ing shortly which will also robustify type handling.

1 Like

Can you share what you did wrong to help others?

Didnt use .ToBson. Did it this way:

            JToken json = JsonConverter.Serialize(data);
            string jsonString = json.ToString();
            byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonString);
            return jsonBytes;

Just submitted a PR. It fixes the library so that long, ulong should now round-trip successfully without having to use bson or other tricks. PR is here : https://github.com/ghi-electronics/TinyCLR-Libraries/pull/710.

If you run into new problems after the next release with this change in it, do please open an issue on github.

3 Likes

And yes, the lib always outputs pretty/human-readable json, which includes indenting and newlines. For maximum compression over the wire or on persistent storage, use bson. It’s the most compact representation.

I didn’t include an option for unformatted json because it would make the lib that much bigger. The more fancy/more options in the lib, the less room there is for your app, so I have tried to code this for the most useful mainstream cases.

3 Likes