Bug 15169 - DataContractJsonSerializer doesn't support deserializing relative Uris
Summary: DataContractJsonSerializer doesn't support deserializing relative Uris
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies ()
Version: master
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-10-03 10:48 UTC by Jonathan Pryor
Modified: 2013-10-03 11:08 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jonathan Pryor 2013-10-03 10:48:00 UTC
DataContractJsonSerializer doesn't support deserializing relative Uris:

  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Runtime.Serialization.Json;
  using System.Runtime.Serialization;
  using System.Text;

  [DataContract]
  public class TestClass
  {
    [DataMember(Name = "members")]
    public Uri MembersRelativeLink { get; set; }
  }

  public class Deserializer
  {
    static public TEntity Deserialize<TEntity>(string json,
        IEnumerable<Type> knownTypes = null)
    {
      var serializer = new DataContractJsonSerializer (typeof (TEntity),
          knownTypes);
      using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (json))) {
        try {
          return (TEntity) serializer.ReadObject (stream);
        } catch (Exception e) {
          throw new NotSupportedException (
              typeof (TEntity).ToString () + " => " + json, e);
        }
      }
    }
  }

  class App {
    public static void Main ()
    { 
      var json = "{\"members\":\"foo/bar/members\"}";

      var obj  = Deserializer.Deserialize<TestClass>(json);
      Console.WriteLine ("obj.MembersRelativeLink={0}",
          obj.MembersRelativeLink);
    }
  }

Compile:

  mcs wcf-json.cs -r:System.Runtime.Serialization.dll

Expected behavior (.NET):

  obj.MembersRelativeLink=foo/bar/members

Actual behavior (mono):

> Unhandled Exception:
> System.NotSupportedException: TestClass => {"members":"foo/bar/members"} ---> System.Runtime.Serialization.SerializationException: Deserialization has failed ---> System.UriFormatException: Invalid URI: The format of the URI could not be determined: foo/bar/members
>   at System.Uri..ctor (System.String uriString, Boolean dontEscape) [0x00000] in <filename unknown>:0 
>   at System.Uri..ctor (System.String uriString) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.Json.JsonSerializationReader.ReadObject (System.Type type) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.Json.TypeMap.Deserialize (System.Runtime.Serialization.Json.JsonSerializationReader jsr) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.Json.JsonSerializationReader.ReadObject (System.Type type) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.Json.JsonSerializationReader.ReadRoot () [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.Xml.XmlReader reader, Boolean verifyObjectName) [0x00000] in <filename unknown>:0 
>   --- End of inner exception stack trace ---
>   at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.Xml.XmlReader reader, Boolean verifyObjectName) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.Xml.XmlDictionaryReader reader, Boolean verifyObjectName) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.Xml.XmlDictionaryReader reader) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject (System.IO.Stream stream) [0x00000] in <filename unknown>:0 
>   at Deserializer.Deserialize[TestClass] (System.String json, IEnumerable`1 knownTypes) [0x00000] in <filename unknown>:0 
>   --- End of inner exception stack trace ---
>   at Deserializer.Deserialize[TestClass] (System.String json, IEnumerable`1 knownTypes) [0x00000] in <filename unknown>:0 
>   at App.Main () [0x00000] in <filename unknown>:0