Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all string from JsonReader in ReadJson method?

Tags:

c#

json.net

This is part of my custom json converter:

 public class ExpandoConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(Expando).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;

        /// How can I get all json string from reader at this point like that:
        /// string js= reader.ReadStringToEnd();

I dont want to get all serialization string, I need data of converter's targeting.

Ex:

{.......................{"Id":3,"Name":"MyExpando1"}}

like image 565
Oguz Karadenizli Avatar asked Sep 10 '25 20:09

Oguz Karadenizli


1 Answers

What I managed with this is using JObject and loading the reader into the JObject like so:

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JObject jObject = JObject.Load(reader);
        var dictionary = serializer.Deserialize<Dictionary<string, object>>(jObject);
    }
like image 84
Fritz Tubbing Avatar answered Sep 13 '25 09:09

Fritz Tubbing