MessagePack-CSharp icon indicating copy to clipboard operation
MessagePack-CSharp copied to clipboard

Extra " when calling MessagePackSerializer.ToJson

Open jiatao99 opened this issue 2 years ago • 3 comments

There is an extra " when calling MessagePackSerializer.ConvertToJson for extension code of 100 (typeless)

In MessagePackSerializer.Json.cs at line 435

writer.Write("{\"$type\":\"" + typeNameTokenBuilder.ToString() + "}");

There is an extra " if after type (when no data after type). This will breaks the JSON format. Should be:

writer.Write("{\"$type\":" + typeNameTokenBuilder.ToString() + "}");

Cheers Tao

jiatao99 avatar Jul 28 '22 13:07 jiatao99

Nice catch, @jiatao99. But rather than an extra, wouldn't we say there's one missing?

With you proposed fix, it appears we might generate this JSON: "$type":someTypeName When that's missing quotes. Shouldn't it be "$type":"someTypeName"?

AArnott avatar Jul 28 '22 13:07 AArnott

Here is the output:

{"column1":[{"value1":[{"$type":""System.Decimal"},"1.001"]}],"column2":[{"value2":[null,"text"]}]}

see {"$type":""System.Decimal"} the problem is :""String.Decimal"

whenever you use TypelessFormatter without write anything out in your own formatter, it output "" after :

jiatao99 avatar Jul 28 '22 13:07 jiatao99

Oh, so typeNameTokenBuilder.ToString() must emit the surrounding quotes itself. Nice find.

Wanna send the PR?

AArnott avatar Jul 28 '22 13:07 AArnott

I would be happy to send the PR if no one else has done it yet

ImanNavidi avatar Aug 24 '22 21:08 ImanNavidi

Thank you, @ImanNavidi

AArnott avatar Aug 25 '22 03:08 AArnott