TypeConverter Message

339 views
Skip to first unread message

nzd...@gmail.com

unread,
Jul 19, 2011, 5:28:35 AM7/19/11
to ravendb
I saved my list of objects, but upon reading it back I get this:

Could not convert string
'WAPL.BBMGradeControl.Domain.AnalyteSourceResult' to dictionary key
type 'WAPL.BBMGradeControl.Domain.AnalyteSourceResult'. Create a
TypeConverter to convert from the string to the key type object.

Ayende Rahien

unread,
Jul 19, 2011, 5:30:17 AM7/19/11
to rav...@googlegroups.com
That seems strange, can you share the full exception message?

nzd...@gmail.com

unread,
Jul 19, 2011, 9:01:56 AM7/19/11
to ravendb
Will this help:

"Newtonsoft.Json.JsonSerializationException: Could not convert string
'WAPL.BBMGradeControl.Domain.AnalyteSourceResult' to dictionary key
type 'WAPL.BBMGradeControl.Domain.AnalyteSourceResult'. Create a
TypeConverter to convert from the string to the key type object. --->
Newtonsoft.Json.JsonSerializationException: Error converting value
\"WAPL.BBMGradeControl.Domain.AnalyteSourceResult\" to type
'WAPL.BBMGradeControl.Domain.AnalyteSourceResult'. --->
System.Exception: Could not cast or convert from System.String to
WAPL.BBMGradeControl.Domain.AnalyteSourceResult.\r\n at
Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object
value, Type initialType, Type targetType) in d:\\Development\\Releases\
\Json\\Working\\Src\\Newtonsoft.Json\\Utilities\\ConvertUtils.cs:line
467\r\n at
Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object
initialValue, CultureInfo culture, Type targetType) in d:\\Development\
\Releases\\Json\\Working\\Src\\Newtonsoft.Json\\Utilities\
\ConvertUtils.cs:line 389\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(Object
value, CultureInfo culture, Type targetType) in d:\\Development\
\Releases\\Json\\Working\\Src\\Newtonsoft.Json\\Serialization\
\JsonSerializerInternalReader.cs:line 479\r\n --- End of inner
exception stack trace ---\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(Object
value, CultureInfo culture, Type targetType) in d:\\Development\
\Releases\\Json\\Working\\Src\\Newtonsoft.Json\\Serialization\
\JsonSerializerInternalReader.cs:line 488\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IWrappedDictionary
dictionary, JsonReader reader, JsonDictionaryContract contract, String
id) in d:\\Development\\Releases\\Json\\Working\\Src\\Newtonsoft.Json\
\Serialization\\JsonSerializerInternalReader.cs:line 615\r\n --- End
of inner exception stack trace ---\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IWrappedDictionary
dictionary, JsonRe
ader reader, JsonDictionaryContract contract, String id) in d:\
\Development\\Releases\\Json\\Working\\Src\\Newtonsoft.Json\
\Serialization\\JsonSerializerInternalReader.cs:line 648\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulateDictionary(JsonReader
reader, JsonDictionaryContract contract, String id) in d:\\Development\
\Releases\\Json\\Working\\Src\\Newtonsoft.Json\\Serialization\
\JsonSerializerInternalReader.cs:line 593\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader
reader, Type objectType, JsonContract contract, JsonProperty member,
Object existingValue) in d:\\Development\\Releases\\Json\\Working\\Src\
\Newtonsoft.Json\\Serialization\\JsonSerializerInternalReader.cs:line
387\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader
reader, Type objectType, JsonContract contract, JsonProperty member,
Object existingValue) in d:\\Development\\Releases\\Json\\Working\\Src\
\Newtonsoft.Json\\
Serialization\\JsonSerializerInternalReader.cs:line 223\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueProperty(JsonReader
reader, JsonProperty property, Object target, Boolean
gottenCurrentValue, Object currentValue) in d:\\Development\\Releases\
\Json\\Working\\Src\\Newtonsoft.Json\\Serialization\
\JsonSerializerInternalReader.cs:line 198\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty
property, JsonReader reader, Object target) in d:\\Development\
\Releases\\Json\\Working\\Src\\Newtonsoft.Json\\Serialization\
\JsonSerializerInternalReader.cs:line 552\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object
newObject, JsonReader reader, JsonObjectContract contract, String id)
in d:\\Development\\Releases\\Json\\Working\\Src\\Newtonsoft.Json\
\Serialization\\JsonSerializerInternalReader.cs:line 1028\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulateObject(JsonReader
reader, JsonObjectContract contract, String id) in d:\\Development\
\Releases\\Json\\Working\\Src\\Newtonsoft.Json\\Serialization\
\JsonSerializerInternalReader.cs:line 858\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader
reader, Type objectType, JsonContract contract, JsonProperty member,
Object existingValue) in d:\\Development\\Releases\\Json\\Working\\Src\
\Newtonsoft.Json\\Serialization\\JsonSerializerInternalReader.cs:line
396\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader
reader, Type objectType, JsonContract contract, JsonProperty member,
Object existingValue) in d:\\Development\\Releases\\Json\\Working\\Src\
\Newtonsoft.Json\\Serialization\\JsonSerializerInternalReader.cs:line
223\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueProperty(JsonReader
reader, JsonProperty property, Object target, Boolean
gottenCurrentValue, Object currentValue) in d:\\Development\\Releases\
\Json\\Worki
ng\\Src\\Newtonsoft.Json\\Serialization\
\JsonSerializerInternalReader.cs:line 198\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty
property, JsonReader reader, Object target) in d:\\Development\
\Releases\\Json\\Working\\Src\\Newtonsoft.Json\\Serialization\
\JsonSerializerInternalReader.cs:line 552\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object
newObject, JsonReader reader, JsonObjectContract contract, String id)
in d:\\Development\\Releases\\Json\\Working\\Src\\Newtonsoft.Json\
\Serialization\\JsonSerializerInternalReader.cs:line 1028\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulateObject(JsonReader
reader, JsonObjectContract contract, String id) in d:\\Development\
\Releases\\Json\\Working\\Src\\Newtonsoft.Json\\Serialization\
\JsonSerializerInternalReader.cs:line 858\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader
reader, Type objectType, Jso
nContract contract, JsonProperty member, Object existingValue) in d:\
\Development\\Releases\\Json\\Working\\Src\\Newtonsoft.Json\
\Serialization\\JsonSerializerInternalReader.cs:line 396\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader
reader, Type objectType, JsonContract contract, JsonProperty member,
Object existingValue) in d:\\Development\\Releases\\Json\\Working\\Src\
\Newtonsoft.Json\\Serialization\\JsonSerializerInternalReader.cs:line
223\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(JsonReader
reader, Type objectType, JsonContract contract) in d:\\Development\
\Releases\\Json\\Working\\Src\\Newtonsoft.Json\\Serialization\
\JsonSerializerInternalReader.cs:line 208\r\n at
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader
reader, Type objectType) in d:\\Development\\Releases\\Json\\Working\
\Src\\Newtonsoft.Json\\Serialization\
\JsonSerializerInternalReader.cs:line 120\r\n at Newto
nsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type
objectType) in d:\\Development\\Releases\\Json\\Working\\Src\
\Newtonsoft.Json\\JsonSerializer.cs:line 421\r\n at
Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type
objectType) in d:\\Development\\Releases\\Json\\Working\\Src\
\Newtonsoft.Json\\JsonSerializer.cs:line 413\r\n at
Raven.Client.Connection.JsonExtensions.Deserialize(RavenJObject self,
Type type, DocumentConvention convention) in c:\\Builds\\raven\
\Raven.Client.Lightweight\\Connection\\JsonExtensions.cs:line 30\r\n
at
Raven.Client.Document.InMemoryDocumentSessionOperations.ConvertToEntity[T]
(String id, RavenJObject documentFound, RavenJObject metadata) in c:\
\Builds\\raven\\Raven.Client.Lightweight\\Document\
\InMemoryDocumentSessionOperations.cs:line 399\r\n at
Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity[T]
(String key, RavenJObject document, RavenJObject metadata) in c:\
\Builds\\raven\\Raven.Client.Lightweight\\Document\\InMemoryDocume
ntSessionOperations.cs:line 326\r\n at
Raven.Client.Document.AbstractDocumentQuery`2.Deserialize(RavenJObject
result) in c:\\Builds\\raven\\Raven.Client.Lightweight\\Document\
\AbstractDocumentQuery.cs:line 1440\r\n at
System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()\r\n at
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r
\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r
\n at Raven.Client.Document.AbstractDocumentQuery`2.GetEnumerator()
in c:\\Builds\\raven\\Raven.Client.Lightweight\\Document\
\AbstractDocumentQuery.cs:line 471\r\n at
Raven.Client.Linq.RavenQueryInspector`1.GetEnumerator() in c:\\Builds\
\raven\\Raven.Client.Lightweight\\Linq\\RavenQueryInspector.cs:line
94\r\n at
WAPL.BBMGradeControl.Service.BbmGradeControlService.GetSamples(DateTimeOffset
from, DateTimeOffset to) in C:\\dev\\WAPL\\BBMGradeControl\\trunk\\src\
\WAPL.BBMGradeControl.Service\\BbmGradeControlService.svc.cs:line 105"





On Jul 19, 5:30 pm, Ayende Rahien <aye...@ayende.com> wrote:
> That seems strange, can you share the full exception message?
>

Ayende Rahien

unread,
Jul 19, 2011, 9:03:14 AM7/19/11
to rav...@googlegroups.com
Hm, can you show me how to reproduce this.

nzd...@gmail.com

unread,
Jul 19, 2011, 9:32:41 AM7/19/11
to ravendb
I'll email you a test program.

It seems to happen when there is a large list.

I dare say that I haven't set up something correctly

On Jul 19, 9:03 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Hm, can you show me how to reproduce this.
>
> > reader, JsonProperty property, Object target, Boolean
> > gottenCurrentValue, Object currentValue) in d:\\Development\\Releases\
> > \Json\\Worki
> > ng\\Src\\Newtonsoft.Json\\Serialization\
> > \JsonSerializerInternalReader.cs:line 198\r\n   at
>
> > Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue (JsonProperty
> > property, JsonReader reader, Object target) in d:\\Development\
> > \Releases\\Json\\Working\\Src\\Newtonsoft.Json\\Serialization\
> > \JsonSerializerInternalReader.cs:line 552\r\n   at
>
> > Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(O bject
> > newObject, JsonReader reader, JsonObjectContract contract, String id)
> > in d:\\Development\\Releases\\Json\\Working\\Src\\Newtonsoft.Json\
> > \Serialization\\JsonSerializerInternalReader.cs:line 1028\r\n   at
>
> > Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulat eObject(JsonReader
> > reader, JsonObjectContract contract, String id) in d:\\Development\
> > \Releases\\Json\\Working\\Src\\Newtonsoft.Json\\Serialization\
> > \JsonSerializerInternalReader.cs:line 858\r\n   at
>
> > Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(Jso nReader
> > reader, Type objectType, Jso
> > nContract contract, JsonProperty member, Object existingValue) in d:\
> > \Development\\Releases\\Json\\Working\\Src\\Newtonsoft.Json\
> > \Serialization\\JsonSerializerInternalReader.cs:line 396\r\n   at
>
> > Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInter nal(JsonReader
> > reader, Type objectType, JsonContract contract, JsonProperty member,
> > Object existingValue) in d:\\Development\\Releases\\Json\\Working\\Src\
> > \Newtonsoft.Json\\Serialization\\JsonSerializerInternalReader.cs:line
> > 223\r\n   at
>
> > Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonPr operty(JsonReader
> ...
>
> read more »

nzd...@gmail.com

unread,
Jul 19, 2011, 8:39:44 PM7/19/11
to ravendb
Did you receive my Progam?
> ...
>
> read more »

nzd...@gmail.com

unread,
Jul 20, 2011, 12:37:50 PM7/20/11
to ravendb
Looking at http://ravendb.net/faq/polymorphism

I wonder if my bases class is the issue. It serializes fine, but
deserialization it can't do without a custom deserialiser
> ...
>
> read more »

nzd...@gmail.com

unread,
Jul 20, 2011, 12:43:35 PM7/20/11
to ravendb
Got it - deleted the documents are recreated with the following code:
documentStore.Conventions.CustomizeSerializer = serializer =>
serializer.TypeNameHandling = TypeNameHandling.All;

On Jul 21, 12:37 am, "nzdu...@gmail.com" <nzdu...@gmail.com> wrote:
> Looking athttp://ravendb.net/faq/polymorphism
> ...
>
> read more »

nzd...@gmail.com

unread,
Jul 20, 2011, 12:58:49 PM7/20/11
to ravendb
Spoke to soon,

When rerunning the program - that creats another session and the error
comes back.

Probably need to learn more and will learn more, but some hints would
help.....
> ...
>
> read more »

Ayende Rahien

unread,
Jul 21, 2011, 6:19:07 AM7/21/11
to rav...@googlegroups.com
No, I didn't

nzd...@gmail.com

unread,
Jul 21, 2011, 9:03:17 PM7/21/11
to ravendb
Oh - sent it to aye...@ayende.com - will try again

On Jul 21, 6:19 pm, Ayende Rahien <aye...@ayende.com> wrote:
> No, I didn't
>
> ...
>
> read more »

nzd...@gmail.com

unread,
Jul 23, 2011, 10:25:38 AM7/23/11
to ravendb
The file may have been too big.... I have reduced it by removing the
Resharper stuff...

Please let me know if you still do not receive it
> ...
>
> read more »

Ayende Rahien

unread,
Jul 23, 2011, 2:05:46 PM7/23/11
to rav...@googlegroups.com
Still didn't get that.

nzd...@gmail.com

unread,
Jul 24, 2011, 9:11:28 AM7/24/11
to ravendb
Not sure why it wouldn't get through. Can I suggest you send me
another email address to nick....@issgroup.com.au

On Jul 24, 2:05 am, Ayende Rahien <aye...@ayende.com> wrote:
> Still didn't get that.
>
> ...
>
> read more »

Ayende Rahien

unread,
Jul 24, 2011, 9:23:44 AM7/24/11
to rav...@googlegroups.com
I don't have another email address, put it on yousendit.com or something like that.

nzd...@gmail.com

unread,
Jul 24, 2011, 10:27:33 AM7/24/11
to ravendb
Ok done that...

On Jul 24, 9:23 pm, Ayende Rahien <aye...@ayende.com> wrote:
> I don't have another email address, put it on yousendit.com or something
> like that.
>
> On Sun, Jul 24, 2011 at 4:11 PM, nzdu...@gmail.com <nzdu...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Not sure why it wouldn't get through.  Can I suggest you send me
> > another email address to nick.zdu...@issgroup.com.au
> ...
>
> read more »

Ayende Rahien

unread,
Jul 25, 2011, 11:18:19 AM7/25/11
to rav...@googlegroups.com
I am sorry, I can't build that, there are many dlls missing, such as: FizzWare.NBuilder, ISS.ApplicationDomain.Service
Can you create a smaller test, one that I can run independently?

nzd...@gmail.com

unread,
Jul 25, 2011, 8:05:20 PM7/25/11
to ravendb
Sorry - the latest upload will have everything self contained.

Run TestSerializeToRaven first and then DeserializeFromRaven

Thanks,

Nick

On Jul 25, 11:18 pm, Ayende Rahien <aye...@ayende.com> wrote:
> I am sorry, I can't build that, there are many dlls missing, such
> as: FizzWare.NBuilder, ISS.ApplicationDomain.Service
> Can you create a smaller test, one that I can run independently?
>
> ...
>
> read more »

Ayende Rahien

unread,
Jul 26, 2011, 1:38:56 AM7/26/11
to rav...@googlegroups.com
Okay, now I can figure out what is going on, and it makes total sense.
Here is a small repro:


[TestMethod]
public void SmallRepro()
{
var serialized = JsonConvert.SerializeObject(new Holder
{
Items = new Dictionary<User, Account>
{
{new User {Name = "Ayende"}, new Account {Type = "Alias"}},
{new User {Name = "Oren"}, new Account {Type = "Name"}}
}
});

var dictionary = JsonConvert.DeserializeObject<Holder>(serialized);
}

public class Holder
{
public Dictionary<User, Account> Items { get; set; }
}

public class User
{
public string Name { get; set; }
}
public class Account
{
public string Type { get; set; }
}

The problem is that in JSON, there is no way to represent something like a dictionary of object to object, it is limited to property names for the dictionary.
You need to create a JsonConverter for that dictionary that would serialize this properly.
Better yet, you might want to modify the entity to avoid this sort of dictionary.

nzd...@gmail.com

unread,
Jul 26, 2011, 2:59:51 AM7/26/11
to ravendb
Thanks for that.... will look into it...

On Jul 26, 1:38 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Okay, now I can figure out what is going on, and it makes total sense.
> Here is a small repro:
>
> [TestMethod]
> public void SmallRepro()
> {
>  var serialized = JsonConvert.SerializeObject(new Holder
> {
> Items = new Dictionary<User, Account>
>  {
> {new User {Name = "Ayende"}, new Account {Type = "Alias"}},
>  {new User {Name = "Oren"}, new Account {Type = "Name"}}}
>
>  });
>
> var dictionary = JsonConvert.DeserializeObject<Holder>(serialized);
>
> }
>
> public class Holder
> {
> public Dictionary<User, Account> Items { get; set; }
>
> }
>
> public class User
> {
> public string Name { get; set; }}
>
> public class Account
> {
> public string Type { get; set; }
>
> }
>
> The problem is that in JSON, there is no way to represent something like a
> dictionary of object to object, it is limited to property names for the
> dictionary.
> You need to create a JsonConverter for that dictionary that would serialize
> this properly.
> Better yet, you might want to modify the entity to avoid this sort of
> dictionary.
>
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages