Unexpected Character While Parsing Value Exception!

Dec 4, 2009 at 10:55 AM
Edited Dec 5, 2009 at 11:53 AM

I added to one of my classes a property type BindingList<T> type is a standard type not generic in use.

Now while i send my serialized object to the other side and deserialize it, only the first time i get this exception.

If i try again, problem disapears. Think like you have a button sends the serialized object to the server and server deserializes this object

for specific actions. You push the button once, get the exception, push it again...again..again...no error! I believe this could be caused after i added

the BindingList<T> property cause before there was no such exception.

Serialize/Deserialize Methods:



        public static T DeSerializePro<T>(byte[] dataObject)
            string dataString = Encoding.UTF8.GetString(dataObject, 0, dataObject.Length);
            JsonConverter[] converters = new JsonConverter[] { new BinaryConverter() };
            return JsonConvert.DeserializeObject<T>(dataString, converters);

        public static byte[] SerializePro<T>(T data)
              JsonConverter[] converters = new JsonConverter[] { new BinaryConverter() };
            string dataString = JsonConvert.SerializeObject(data, converters);
            return Encoding.UTF8.GetBytes(dataString);


[EDIT] This happens in every Send of objects, its not BindingList property problem...and i think all began when i inherited my Business

objects from a base class containing an enumeration declaration and the class containing a property of the enum type.

Any suggestions? Anyone?


Dec 6, 2009 at 1:10 AM

I'm going to need to see the JSON that you are deserializing when you get this exception and the class you are deserializing into. The exception details would also be useful.

Dec 6, 2009 at 12:54 PM

One of the Classes and Base class:

public enum BusinessObjectState { None = 0, Insert = 1, Update = 2, Delete = 3 } public abstract class BusinessBase { public BusinessObjectState DatabaseState { set; get; } protected BusinessBase() { this.DatabaseState = BusinessObjectState.None; } } public class Product : BusinessBase { public int Id { set; get; } public string Name { get; set; } public int CategoryId { get; set; } public Product() {} public Product(int id, string name) { this.Id = id; this.Name = name; } }

The Json of the Product i send for Insert. I should state that all Select that i get from the other side gets OK anytime. Problem seems to be when

i send from the "Client" to the "Server" an entity for insertion deletion etc...:

First the DataPackage object where never has problem contains a byte[] Data property with another serialized Json Product object:


And next the Product object Deserializing the Json string before call JsonConvert.DeserializeObject<T>(dataString, converters) and where i get the exception:



Exception: +        $exception    {"Unexpected character encountered while parsing value: \0. Line 1, position 1."}    System.Exception {Newtonsoft.Json.JsonReaderException}


at Newtonsoft.Json.JsonTextReader.ParseValue() in d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonTextReader.cs:line 576
   at Newtonsoft.Json.JsonTextReader.Read() in d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonTextReader.cs:line 302
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType) in d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 87
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) in d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonSerializer.cs:line 383
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) in d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonSerializer.cs:line 375
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) in d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonConvert.cs:line 622
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonConverter[] converters) in d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonConvert.cs:line 600
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonConverter[] converters) in d:\Newtonsoft\Projects\Json\trunk\Src\Newtonsoft.Json\JsonConvert.cs:line 569
   at Wizcom.InOrderII.Sockets.Serialize.DeSerializePro[T](Byte[] dataObject) in D:\Visual Studio Projects\InII\Sockets\HelperClasses.cs:line 1085
   at Wizcom.InOrderII.Sockets.SocketServer.UpdateOrderInDatabase(DataReceivedEventArgs e, State stateClient) in D:\Visual Studio Projects\InII\Sockets\Sockets.cs:line 1125

As i can guess the Unexpected Character is the \ and the \0, but where does this come from?

Then lets say you try again pushing the same button for same operation and everything goes as expected and as before. This is the Json string:


If you could help, thank you.

Dec 6, 2009 at 10:13 PM

This is wrong:


Below is what you want for it to be successfully deserialized:


The problem in the first case is your string has been escaped at some point. I would guess the problem is occurring around the point on the client were you put together the JSON.

Dec 6, 2009 at 10:36 PM
Edited Dec 6, 2009 at 10:55 PM

By that is when im calling the Serialize method of mine? could this be because I'm calling it inside an object initializer?

And why in the 2nd time this works fine!? i guess i will make some changes and let you know.


I guess your guess was correct. I removed the Serialize method i had while constructing the DataPackage object and now works.

Looks like it doesn't work so good in there for a reason:) thanks, you helped on discovering the problem.


Dec 7, 2009 at 6:49 PM

Grrrr.... c'mon, how can this be showed up again???? I don't do nothing wrong! I removed the binary converter this time and here is the


Json string that goes into byte[] property:


goes to: Encoding.UTF8.GetBytes(dataString);


Then serialize DataPackage includes this as byte[] in a property Data as you can see:






And in the other side comes the DataPackage:






And then Deserialize the Data Property:


damn....all next requests working fine!!!!!

Any explanation....????? James I have to fix this!!! HELP!


Dec 7, 2009 at 7:53 PM

Your problem isn't in Json.NET.

If you really really can't solve it and time is running short then you could always hack it.

You could do something like this on your receiving end to deal with the first request:

if (json.StartsWith(@"\0"))
  json = json.Replace(@"\0", "");
  json = json.Replace("@"\""", @"""");

Dec 7, 2009 at 8:52 PM
Edited Dec 7, 2009 at 10:20 PM

I just bumping my head over the wall to understand how and why this came up!!

Strange is when i try to investigate the Json, shows empty. And the if never evaluates true!

Dec 23, 2009 at 4:22 PM

I think I have had the same problem.

This was just for educational purposes.

A Simple Client - Server application using TCP. I create an Object on client-side , Serialize it using JsonConvert.SerializeObject(..) then convert the JSON String to a byte[] using ASEN.GetBytes(JSONString) and then transmit these bytes over the wire.

On the Server side when these bytes are received and converted back to a string using System.Text.ASCIIEncoding.ASCII.GetString(byte[]); and if the byte[] array size is more than required for that string, then \0 characters get appended to the string.

This string which has valid JsonString within it appended with \0 values cannot be deserialized by json.NET deserializer.

Ofcourse,what James suggested in the previous post is a way around this. Or just simply trim the string and then deserialize it.

But what worries me is, can json.NET recognize ALL escape characters?  What if I want an String property within my object to be serialized which does keep in mind the format of how the string was entered, such as tabs and new lines?