Newtonsoft Jsons Deserialize不会解析$ ref对象

时间:2017-07-10 14:18:16

标签: json serialization reference json.net

我知道它应该有用,但它没有,我不知道这有什么不对。 根["参数"] - 字典正确反序列化,但根["路径"] [" / alliances /"] [参数参考列表] [ "得到"] ["参数"]是一大堆空的。

我缩短了内容。 Origninal是here。 从(我自己的)SwaggerParameter.Deserializable(在Root中正常工作)切换到JSchema只是从生成null变为抛出异常"无法解析模式引用'# /参数/数据源'"

[TestClass()]
public class RootNode_Tests
{
    static string s_sContent = @"{
       ""parameters"":{
            ""X-User-Agent"":{
                ""description"":""Client identifier, takes precedence over User-Agent"",
                ""in"":""header"",
                ""name"":""X-User-Agent"",
                ""type"":""string""
            },
            ""datasource"":{
                ""default"":""tranquility"",
                ""description"":""The server name you would like data from"",
                ""enum"":[
                    ""tranquility"",
                    ""singularity""
                ],
                ""in"":""query"",
                ""name"":""datasource"",
                ""type"":""string""
            }
        },
        ""paths"":{
            ""/alliances/"":{
                ""get"":{
                    ""operationId"":""get_alliances"",
                    ""parameters"":[
                        {
                            ""$ref"":""#/parameters/datasource""
                        },
                        {
                            ""$ref"":""#/parameters/X-User-Agent""
                        }
                    ]}
                }
            }
        }";
    [TestMethod]
    public void Deserialize_Test()
    {

        TestRoot tr = JsonConvert.DeserializeObject<TestRoot>(s_sContent, new JsonSerializerSettings()
        {
            MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead, // shouldnt be neccessary
            Formatting = Formatting.Indented,
            PreserveReferencesHandling = PreserveReferencesHandling.All,
            TraceWriter = new DbgPrintTracer()
        });
    }
    public class DbgPrintTracer : ITraceWriter
    {
        public TraceLevel LevelFilter => TraceLevel.Verbose;
        public void Trace(TraceLevel level, string message, Exception ex) => Debug.Print($"{level}: {message} ({ex})");
    }
    public abstract class _TestBase
    {
        [JsonExtensionData]
        public Dictionary<string, object> m_dictAdditional { get; protected set; }
    }
    public class TestRoot : _TestBase
    {
        [JsonProperty("parameters")]
        //[JsonConverter(typeof(ParamCollectionConverter))]
        //public Dictionary<string,SwaggerParameter.Deserializable> m_dictParameters { get; protected set; } /* DESERIALIZES PROPERLY */
        public Dictionary<string, JSchema> m_dictParameters { get; protected set; }/* DESERIALIZES PROPERLY TOO */
        [JsonProperty("paths")]
        public Dictionary<string, Paths> m_dictPaths { get; protected set; }
    }
    public class Paths : Dictionary<string, Method>
    {
    }
    public class Method : _TestBase
    {
        [JsonProperty("parameters")]
        //  [JsonConverter(typeof(ParamCollectionConverter))]
        // public List<SwaggerParameter.Deserializable> m_lstParams { get; protected set; }    /* ONLY NULL's, NO EXEPTIONS */
        public List<JSchema> m_lstParams { get; protected set; } /* EXCEPTION: Couldnt resolve reference-Path. */
    }
}

0 个答案:

没有答案