Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:21:52+00:00 2026-05-26T21:21:52+00:00

I need to serialize and deserialize an object that contains an System.Uri property using

  • 0

I need to serialize and deserialize an object that contains an System.Uri property using the latest (4.0.3) Json.NET library.

The following code demonstrates the problem:

string input = "http://test.com/%22foo+bar%22";
Uri uri = new Uri(input);
string json = JsonConvert.SerializeObject(uri);
Uri output = JsonConvert.DeserializeObject<Uri>(json);

The DeserializeObject method throws an JsonReaderException. This works fine with 4.0.2.

I’ve submitted an issue on codeplex with tests and patch to fix the issue, but it seems it takes a bit for the author to release a fixed version.

Meanwhile is there anything i can do ( using JsonSettings or anything else ) to make the latest version work as expected?

A few options i have so far:

  1. stick to 4.0.2 – new nuget packages depend on 4.0.3
  2. change uri to string – i rather go with option 1 and manually managed pkg dependencies
  3. use custom build with patch applied – this is what i’m doing now but i hate the idea of overriding the nuget package’s assemblies.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:21:53+00:00Added an answer on May 26, 2026 at 9:21 pm

    You can write a helper class that circumvents the usual handling of System.Uri and treats it purely as a string, using the Uri.OriginalString property.

    Here’s a code sample that does exactly that with a converter class derived from Json.Net’s JsonConverter.

    OriginalString Property (System.Uri) @ MSDN

    One caveat is that you must update all places where you use JsonConvert to include the helper class as one of the extra JsonConverter parameters.

    I’ve also added an example using Uri as a member variable in a class, to demonstrate that one wouldn’t necessarily have to override the attributes on a class, though it might be more convenient for you. If so, you could use [JsonConverter(UriConverter)] as an attribute on members that need it.

    using Newtonsoft.Json;
    
    namespace JsonUriSerializeTest
    {
        class Program
        {
            public class UriConverter : JsonConverter
            {
                public override bool CanConvert(Type objectType)
                {
                    return objectType.Equals(typeof(Uri));
                }
    
                public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                {
                    if (reader.TokenType == JsonToken.String)
                    {
                        return new Uri((string)reader.Value);
                    }
    
                    if (reader.TokenType == JsonToken.Null)
                    {
                        return null;
                    }
    
                    throw new InvalidOperationException("Unhandled case for UriConverter. Check to see if this converter has been applied to the wrong serialization type.");
                }
    
                public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                {
                    if (null == value)
                    {
                        writer.WriteNull();
                        return;
                    }
    
                    if (value is Uri)
                    {
                        writer.WriteValue(((Uri)value).OriginalString);
                        return;
                    }
    
                    throw new InvalidOperationException("Unhandled case for UriConverter. Check to see if this converter has been applied to the wrong serialization type.");
                }
            }
    
            public class UriPair
            {
                public string label { get; set; }
                public Uri first { get; set; }
                public Uri second { get; set; }
    
                public void Display()
                {
                    Console.WriteLine(string.Format("label:  {0}", label));
                    Console.WriteLine(string.Format("first:  {0}", first));
                    Console.WriteLine(string.Format("second: {0}", second));
                }
            }
    
            static void Main(string[] args)
            {
                string input = "http://test.com/%22foo+bar%22";
                Uri uri = new Uri(input);
                string json = JsonConvert.SerializeObject(uri, new UriConverter());
                Uri output = JsonConvert.DeserializeObject<Uri>(json, new UriConverter());
    
                Console.WriteLine(input);
                Console.WriteLine(output.ToString());
                Console.WriteLine();
    
                UriPair pair = new UriPair();
                pair.label = input;
                pair.first = null;
                pair.second = new Uri(input);
                string jsonPair = JsonConvert.SerializeObject(pair, new UriConverter());
                UriPair outputPair = JsonConvert.DeserializeObject<UriPair>(jsonPair, new UriConverter());
    
                outputPair.Display();
                Console.WriteLine();
    
                Console.ReadKey();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that I need to binary serialize. The class contains one
I'm trying to use the DataContractJsonSerializer to serialize and deserialize a c# object that
I need to deserialize some JavaScript object represented in JSON to an appropriate C#
I am using a C# object to Serialize/Deserialize XML. I would like to add
What I have is a custom settings file that I serialize/deserialize using an XmlSerializer
I need to serialize a .NET DateTime value in a protocol buffers message. My
I have a simple Java class that I need to serialize to be stored
I have a class that I need to be able to serialize to a
I am faced with the following problem. I need to (de)serialize (binary) a stream
I need to serialize some object graphs to disc What difficulties am I likely

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.