Comments (6)
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.
I've got the same problem here, it would be helpful if deserialization is supported as well!
from restsharp.serializers.newtonsoft.json.
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.
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.
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.
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)
- Why does the latest build REQUIRE Json,NET >= 8.0.2? HOT 3
- Cannot resolve the reference HOT 3
- [FYI] Package doesn't serialize anymore, but you won't believe what RestSharp did next! HOT 1
- NuGet package deprecation HOT 2
- monoandroid missed HOT 1
- strongname
- Request Upgrade to .NET Standard 2.0 HOT 1
- No longer installs on .NET 4.6 HOT 3
- Net standard support not working HOT 1
- Binary incompatible with latest RestSharp HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from restsharp.serializers.newtonsoft.json.