Coder Social home page Coder Social logo

Comments (6)

adamfisher avatar adamfisher commented on June 22, 2024 2

This has been added in version 1.5.0 thanks to the code @quetzalcoatl provided 👍.

https://github.com/adamfisher/RestSharp.Serializers.Newtonsoft.Json/releases/tag/1.5.0

from restsharp.serializers.newtonsoft.json.

Rolorob avatar Rolorob commented on June 22, 2024 1

I've got the same problem here, it would be helpful if deserialization is supported as well!

from restsharp.serializers.newtonsoft.json.

Rolorob avatar Rolorob commented on June 22, 2024 1

Thanks, I handled this the same way as you described but forgot to feed this back, sorry :) Thanks for your response, hope it helps others. Maybe I'll submit a PR later.

from restsharp.serializers.newtonsoft.json.

quetzalcoatl avatar quetzalcoatl commented on June 22, 2024

The public class RestRequest : IRestRequest from RestSharp does not provide any way for that.
However, it seems that RestClient may be reconfigured to use a different serializers for each response mime type - see i.e. https://bytefish.de/blog/restsharp_custom_json_serializer/#using-the-custom-deserializer-for-incoming-responses or on github https://github.com/bytefish/bytefish.de/blob/master/blog/restsharp_custom_json_serializer.md

from restsharp.serializers.newtonsoft.json.

quetzalcoatl avatar quetzalcoatl commented on June 22, 2024

In case the link rots, here's the code snippet from that article, with a tiny change in the name of implemented interfaces (since RestSharp changed a bit since then)

// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Newtonsoft.Json;
using RestSharp.Serializers;
using System.IO;
using RestSharp.Deserializers;

namespace GcmSharp.Serialization
{
    public class NewtonsoftJsonSerializer : ISerializer, IDeserializer
    {
        private Newtonsoft.Json.JsonSerializer serializer;

        public NewtonsoftJsonSerializer(Newtonsoft.Json.JsonSerializer serializer)
        {
            this.serializer = serializer;
        }

        public string ContentType
        {
            get { return "application/json"; } // Probably used for Serialization?
            set { }
        }

        public string DateFormat { get; set; }

        public string Namespace { get; set; }

        public string RootElement { get; set; }

        public string Serialize(object obj)
        {
            using (var stringWriter = new StringWriter())
            {
                using (var jsonTextWriter = new JsonTextWriter(stringWriter))
                {
                    serializer.Serialize(jsonTextWriter, obj);

                    return stringWriter.ToString();
                }
            }
        }

        public T Deserialize<T>(RestSharp.IRestResponse response)
        {
            var content = response.Content;

            using (var stringReader = new StringReader(content))
            {
                using (var jsonTextReader = new JsonTextReader(stringReader))
                {
                    return serializer.Deserialize<T>(jsonTextReader);
                }
            }
        }

        public static NewtonsoftJsonSerializer Default
        {
            get
            {
                return new NewtonsoftJsonSerializer(new Newtonsoft.Json.JsonSerializer()
                {
                    NullValueHandling = NullValueHandling.Ignore,
                });
            }
        }
    }
}

and how to use it:

    var jser = Newtonsoft.Json.JsonSerializer.CreateDefault();
    // above: use any way to get the serializer, set its options, etc

    var wrapper = new GcmSharp.Serialization.NewtonsoftJsonSerializer(jser);

    request.JsonSerializer = wrapper;

    client.AddHandler("application/json", wrapper);
    client.AddHandler("text/json", wrapper);
    client.AddHandler("text/x-json", wrapper);
    client.AddHandler("text/javascript", wrapper);
    client.AddHandler("*+json", wrapper);
    // and so on for other MIME types you want to process

from restsharp.serializers.newtonsoft.json.

adamfisher avatar adamfisher commented on June 22, 2024

It seems this was also addressed with PR #4 but I forgot about it after approving it 😢

from restsharp.serializers.newtonsoft.json.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.