question

kishorekumar-4126 avatar image
0 Votes"
kishorekumar-4126 asked kishorekumar-4126 edited

How to resolve .net core 3.1 json deserialization exception.

Dear Experts,

Please help me to resolve this below exception.

I am using .net core 3.1 System.Text.Json namespace, I don't want to use third party components.


 string jsonStr= "{  \"name1\": \"test\",  \"key\": \"test2\",  \"args\": {    \"value\": \"%PDF-1.4 Scanned ImagePDF%Non-Encryption3 0 obj<</Type /Page/Parent 1 0 R/Resources 4 0 R/Contents 5 0 R/MediaBox [0 0 613 790]>>endobj4 0 obj<</ProcSet [/PDF /ImageB]/XObject << /Img1 6 0 R >>>>endobj5 0 obj<</Length 27>>stream613 0 0 790 0 0 cm/Img1 Doendstreamendobj6 0 obj<</Type /XObject/Subtype /Image/Name /Img1/Filter /CCITTFaxDecode/DecodeParms <<  /K -1 /Columns 1704 /Rows 2196 >>/Width 1704/Height 2196/BitsPerComponent 1/ColorSpace /DeviceGray/Length 7 0 R>>stream_�������W]������}�Y���������W��_����������)�����������������������������������������!|�P!E�;!a̝��0�fC@@ h`�gNKb���@����<&�M�au�D<�;D??KDB:D\"*��?�oA�A����}&�6�;�zo����:o����+�'�m'���������W�Q��C#G���*�`C�xDX��PEx����N�F�Π�0\\�j ���=<q[c��\\ xA��a��M�m��\t��/�^�ڽ�A���Z!0������8\"?������Bg���P�`�?�^\"a�o�,6�> ����� l�PA�&��`�A�>�a�?�uG���_���a{ @0�I��dŲ�����I��؞��'݃I�>�u���{�����ޟ����5��a�������0��^���L������=��YIA����8�Z ���\"  }}";
     var data = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonStr);

The above code not able to desterilize to an Dictionary<string, object> type.
Here in the json string value key contains the pdf file data, it may have some unprintable characters also.

Can you please help us to resolve deserilize to a dictionary.

Best Regards,
Kumar




dotnet-csharp
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Are you sure that this JSON string is valid? How was it obtained?

Show your code and the error messages.

0 Votes 0 ·

I prepared json string manually. the json data contains non printable characters, deserialization is failed.
Is there any way to De serialize the json data which contains non printable characters.

Regards,
Kumar

0 Votes 0 ·

Maybe you should consider a different format of value. For example, you can get an array (in case of PDF files, use File.ReadAllBytes), represent it as text using Convert.ToBase64String, then use it in JSON. The deserialisation should work. You can use Convert.FromBase64String to obtain the PDF bytes after deserialisation.


0 Votes 0 ·
Show more comments

0 Answers