Skip to main content
Update link, blog post and code were moved to my GitHub account.
Source Link
Christophe Geers
  • 9.1k
  • 3
  • 40
  • 57

For this sort of operation, I always look at TweetSharp and how it handles it.

For example, TweetSharp uses a TwitterGeoConverter.cs to serialise/deserialise the TwitterGeoLocation.GeoCoordinates type to/from JSON: https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/Converters/TwitterGeoConverter.cs

The key methods in this converter are:

  • CanConvert - should this converter be used on this member
  • WriteJson - handles the object to string output
  • ReadJson - handles the string to object parsing

The converters themselves are registered with JSON.Net using JsonSerializerSettings - e.g:

new JsonSerializerSettings
                   {
                       MissingMemberHandling = MissingMemberHandling.Ignore,
                       NullValueHandling = NullValueHandling.Ignore,
                       DefaultValueHandling = DefaultValueHandling.Include,
                       ContractResolver = new JsonConventionResolver(),
                       Converters = new List<JsonConverter>
                                        {
                                            new TwitterDateTimeConverter(),
                                            new TwitterWonkyBooleanConverter(),
                                            new TwitterGeoConverter()
                                        }
                   })

(from https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/SerializerBase.cs)


Alternatively, you can also register converters using attributes - see http://cgeers.com/2011/09/25/writing-a-custom-json-net-datetime-converter/https://github.com/geersch/JsonNetDateTimeConverter


Or... if the case is very simple and if you own the source code - then if you simply want to ignore some properties during the serialisation, then there is a [JsonIgnore] attribute available for the properties you want to skip.

For this sort of operation, I always look at TweetSharp and how it handles it.

For example, TweetSharp uses a TwitterGeoConverter.cs to serialise/deserialise the TwitterGeoLocation.GeoCoordinates type to/from JSON: https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/Converters/TwitterGeoConverter.cs

The key methods in this converter are:

  • CanConvert - should this converter be used on this member
  • WriteJson - handles the object to string output
  • ReadJson - handles the string to object parsing

The converters themselves are registered with JSON.Net using JsonSerializerSettings - e.g:

new JsonSerializerSettings
                   {
                       MissingMemberHandling = MissingMemberHandling.Ignore,
                       NullValueHandling = NullValueHandling.Ignore,
                       DefaultValueHandling = DefaultValueHandling.Include,
                       ContractResolver = new JsonConventionResolver(),
                       Converters = new List<JsonConverter>
                                        {
                                            new TwitterDateTimeConverter(),
                                            new TwitterWonkyBooleanConverter(),
                                            new TwitterGeoConverter()
                                        }
                   })

(from https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/SerializerBase.cs)


Alternatively, you can also register converters using attributes - see http://cgeers.com/2011/09/25/writing-a-custom-json-net-datetime-converter/


Or... if the case is very simple and if you own the source code - then if you simply want to ignore some properties during the serialisation, then there is a [JsonIgnore] attribute available for the properties you want to skip.

For this sort of operation, I always look at TweetSharp and how it handles it.

For example, TweetSharp uses a TwitterGeoConverter.cs to serialise/deserialise the TwitterGeoLocation.GeoCoordinates type to/from JSON: https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/Converters/TwitterGeoConverter.cs

The key methods in this converter are:

  • CanConvert - should this converter be used on this member
  • WriteJson - handles the object to string output
  • ReadJson - handles the string to object parsing

The converters themselves are registered with JSON.Net using JsonSerializerSettings - e.g:

new JsonSerializerSettings
                   {
                       MissingMemberHandling = MissingMemberHandling.Ignore,
                       NullValueHandling = NullValueHandling.Ignore,
                       DefaultValueHandling = DefaultValueHandling.Include,
                       ContractResolver = new JsonConventionResolver(),
                       Converters = new List<JsonConverter>
                                        {
                                            new TwitterDateTimeConverter(),
                                            new TwitterWonkyBooleanConverter(),
                                            new TwitterGeoConverter()
                                        }
                   })

(from https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/SerializerBase.cs)


Alternatively, you can also register converters using attributes - see https://github.com/geersch/JsonNetDateTimeConverter


Or... if the case is very simple and if you own the source code - then if you simply want to ignore some properties during the serialisation, then there is a [JsonIgnore] attribute available for the properties you want to skip.

added 35 characters in body
Source Link
Stuart
  • 66.9k
  • 7
  • 116
  • 168

For this sort of operation, I always look at TweetSharp and how it handles it.

For example, TweetSharp uses a TwitterGeoConverter.cs to serialise/deserialise the TwitterGeoLocation.GeoCoordinates type to/from JSON: https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/Converters/TwitterGeoConverter.cs

The key methods in this converter are:

  • CanConvert - should this converter be used on this member
  • WriteJson - handles the object to string output
  • ReadJson - handles the string to object parsing

The converters themselves are registered with JSON.Net using JsonSerializerSettings - e.g:

new JsonSerializerSettings
                   {
                       MissingMemberHandling = MissingMemberHandling.Ignore,
                       NullValueHandling = NullValueHandling.Ignore,
                       DefaultValueHandling = DefaultValueHandling.Include,
                       ContractResolver = new JsonConventionResolver(),
                       Converters = new List<JsonConverter>
                                        {
                                            new TwitterDateTimeConverter(),
                                            new TwitterWonkyBooleanConverter(),
                                            new TwitterGeoConverter()
                                        }
                   })

(from https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/SerializerBase.cs)


Alternatively, you can also register converters using attributes - see http://cgeers.com/2011/09/25/writing-a-custom-json-net-datetime-converter/


Or... if thisthe case is very simple and if you own the source code - then if you simply want to ignore some properties during the serialisation, then there is a [JsonIgnore] attribute available for the properties you want to skip.

For this sort of operation, I always look at TweetSharp and how it handles it.

For example, TweetSharp uses a TwitterGeoConverter.cs to serialise/deserialise the TwitterGeoLocation.GeoCoordinates type to/from JSON: https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/Converters/TwitterGeoConverter.cs

The key methods in this converter are:

  • CanConvert - should this converter be used on this member
  • WriteJson - handles the object to string output
  • ReadJson - handles the string to object parsing

The converters themselves are registered with JSON.Net using JsonSerializerSettings - e.g:

new JsonSerializerSettings
                   {
                       MissingMemberHandling = MissingMemberHandling.Ignore,
                       NullValueHandling = NullValueHandling.Ignore,
                       DefaultValueHandling = DefaultValueHandling.Include,
                       ContractResolver = new JsonConventionResolver(),
                       Converters = new List<JsonConverter>
                                        {
                                            new TwitterDateTimeConverter(),
                                            new TwitterWonkyBooleanConverter(),
                                            new TwitterGeoConverter()
                                        }
                   })

(from https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/SerializerBase.cs)


Alternatively, you can also register converters using attributes - see http://cgeers.com/2011/09/25/writing-a-custom-json-net-datetime-converter/


Or... if this case is very simple - if you simply want to ignore some properties during the serialisation, then there is a [JsonIgnore] attribute available for the properties you want to skip.

For this sort of operation, I always look at TweetSharp and how it handles it.

For example, TweetSharp uses a TwitterGeoConverter.cs to serialise/deserialise the TwitterGeoLocation.GeoCoordinates type to/from JSON: https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/Converters/TwitterGeoConverter.cs

The key methods in this converter are:

  • CanConvert - should this converter be used on this member
  • WriteJson - handles the object to string output
  • ReadJson - handles the string to object parsing

The converters themselves are registered with JSON.Net using JsonSerializerSettings - e.g:

new JsonSerializerSettings
                   {
                       MissingMemberHandling = MissingMemberHandling.Ignore,
                       NullValueHandling = NullValueHandling.Ignore,
                       DefaultValueHandling = DefaultValueHandling.Include,
                       ContractResolver = new JsonConventionResolver(),
                       Converters = new List<JsonConverter>
                                        {
                                            new TwitterDateTimeConverter(),
                                            new TwitterWonkyBooleanConverter(),
                                            new TwitterGeoConverter()
                                        }
                   })

(from https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/SerializerBase.cs)


Alternatively, you can also register converters using attributes - see http://cgeers.com/2011/09/25/writing-a-custom-json-net-datetime-converter/


Or... if the case is very simple and if you own the source code - then if you simply want to ignore some properties during the serialisation, then there is a [JsonIgnore] attribute available for the properties you want to skip.

Source Link
Stuart
  • 66.9k
  • 7
  • 116
  • 168

For this sort of operation, I always look at TweetSharp and how it handles it.

For example, TweetSharp uses a TwitterGeoConverter.cs to serialise/deserialise the TwitterGeoLocation.GeoCoordinates type to/from JSON: https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/Converters/TwitterGeoConverter.cs

The key methods in this converter are:

  • CanConvert - should this converter be used on this member
  • WriteJson - handles the object to string output
  • ReadJson - handles the string to object parsing

The converters themselves are registered with JSON.Net using JsonSerializerSettings - e.g:

new JsonSerializerSettings
                   {
                       MissingMemberHandling = MissingMemberHandling.Ignore,
                       NullValueHandling = NullValueHandling.Ignore,
                       DefaultValueHandling = DefaultValueHandling.Include,
                       ContractResolver = new JsonConventionResolver(),
                       Converters = new List<JsonConverter>
                                        {
                                            new TwitterDateTimeConverter(),
                                            new TwitterWonkyBooleanConverter(),
                                            new TwitterGeoConverter()
                                        }
                   })

(from https://github.com/danielcrenna/tweetsharp/blob/master/src/net40/TweetSharp.Next/Serialization/SerializerBase.cs)


Alternatively, you can also register converters using attributes - see http://cgeers.com/2011/09/25/writing-a-custom-json-net-datetime-converter/


Or... if this case is very simple - if you simply want to ignore some properties during the serialisation, then there is a [JsonIgnore] attribute available for the properties you want to skip.