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 8052943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:41:54+00:00 2026-06-05T07:41:54+00:00

Using the Newtonsoft.Json library, imagine I have got public class Test { public Object

  • 0

Using the Newtonsoft.Json library, imagine I have got

public class Test
{
   public Object Obj { get; set; }
}

Now, attempting to serialize this like so

var json = JsonConvert.SerializeObject(new Test(){ Obj = new Uri(@"http://www.google.com") });

…will give me the following JSON

{
    "Obj": "http://www.google.com"
}

Which is clearly not enough information to deserialize this back into a Uri object, and in fact, attempting to deserialize it will give me a String object instead.

Is there any existing way to correctly serialize and deserialize the type information here so that the object will be read back in as a Uri instead of a String? In this particular case, I am only attempting to interop with a .NET application and it is extremely important that the exact types are deserialized.

Thanks in advance.

  • 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-06-05T07:41:57+00:00Added an answer on June 5, 2026 at 7:41 am

    If you want to convert the string back to the Uri, you can use custom converter attribute

    The converter

    public class UriConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return true;
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.String)
            {
                //try to create uri out of the string
                Uri uri;
                if(Uri.TryCreate(reader.Value.ToString(), UriKind.Absolute, out uri))
                {
                    return uri;
                }
                //just a string -> return string value
                return reader.Value;
            }
    
            if (reader.TokenType == JsonToken.Null)
            {
                return null;
            }
    
            throw new InvalidOperationException("Unable to process JSON");
        }
    
        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("Unable to process JSON");
        }
    }
    

    And the use attribute

     [JsonConverter(typeof(UriConverter))]
     public object Obj {get;set;}
    

    You should then be able to determine whether the underlying object is Uri like

      var data = JsonConvert.DeserializeObject<YourObject>(yourJSONString);
      if (data.Obj is Uri)
      {
           ... add logic here
      }
      else
      {
           ... not Uri different logic
      }
    

    You can also check out this article for more information Json.NET Uri (de)serialization errors

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've serialized an object using Newtonsoft's JSON serializer, and the DateTime has come through
I'm attempting to serialize a class hierarchy in Newtonsoft Json using C#. My class
I have a working application using Json.NET (newtonsoft) as a custom serializer. Currently I'm
I am using Newtonsoft Json library to parse json but i don't know how
I am using Newtonsoft.Json to Deserialize json string to Object, but I can't judge
I'm trying to get the following json (see below) deserialized (using newtonsoft json serializer)
Parsing Google Plus JSON objects Using C# and Newtonsoft.Json library how one can parse
Is there is any other way deserialize JSON string,rather than using the Newtonsoft library?
I am using Newtonsoft's Json when i serialze a date time property i get
Currently I am using the terrific Linq 2 Json.net (by newtonsoft), which is a

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.