Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

No deserializer? #8

Closed
BHare1985 opened this issue Apr 5, 2018 · 6 comments
Closed

No deserializer? #8

BHare1985 opened this issue Apr 5, 2018 · 6 comments
Assignees

Comments

@BHare1985
Copy link

Is there a specific reason why you don't also include a deserializer as well?
Would be nice to get an all in one solution.

Specifically, I don't like how the default RestSharp deserializer doesn't support enums the same way as json.net

@Rolorob
Copy link

Rolorob commented Apr 19, 2018

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

@quetzalcoatl
Copy link

quetzalcoatl commented May 14, 2018

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

@quetzalcoatl
Copy link

quetzalcoatl commented May 14, 2018

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

@Rolorob
Copy link

Rolorob commented May 14, 2018

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.

@adamfisher adamfisher self-assigned this Jun 17, 2018
@adamfisher
Copy link
Owner

adamfisher commented Jun 17, 2018

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

@adamfisher
Copy link
Owner

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants