JSON.net CF 3.5,验证JSON是否完整 [英] JSON.net CF 3.5, verify that JSON is complete

查看:128
本文介绍了JSON.net CF 3.5,验证JSON是否完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在3.5 CF设置中使用Json.net,但在验证字符串确实是完整的JSON时遇到问题.

I'm using Json.net in a 3.5 CF setting and have a problem verifying that a string is indeed complete JSON.

我正在使用:

var o = JObject.Parse(incomingString);

,如果JSON不完整-但并非总是如此,它将返回null.如果JSON是大多数形式",它将正确解析.这个简单的示例返回一个对象:

which will return null if the JSON is incomplete - but not always. If the JSON is something "mostly formed", it will parse correctly. This simple example returns an object:

{ "Name":"Bob", "Pets":[ {"Type":"Cat", "Name":"Pudge" 

但是如果我在其他地方破坏了JSON,它会按预期返回null.

but if I break the JSON elsewhere, it returns null as expected.

{ "Name":"Bob", "Pets":[ {"Type":"Cat", "Nam

没有右括号,似乎假设"了这些括号并返回了正确的JObject,但是由于此JSON数据正在流式传输,因此我需要在处理之前确认所有括号都匹配.

With no closing brackets it seems to "assume" those brackets and returns a proper JObject, but since this JSON data is streaming in I need to verify that all brackets are matched before I process it.

在我们有限的沙箱中,我似乎没有较新的API上提供的任何验证方法.在处理它之前是否有验证我是否拥有整个JSON的建议?谢谢.

In our limited sandbox, I don't seem to have any of the validation methods available on the newer APIs. Any suggestions for verifying that I have the entire JSON before I process it? Thanks.

推荐答案

好,它已在Json.NET 已修复JToken加载和解析方法不会检查内容是否不完整,但您只能使用35r8,因为这是支持紧凑型框架的最新版本.在这种情况下,以下静态助手方法将检查起始深度和结束深度是否匹配:

OK, it was fixed in Json.NET 4.0.1: Fixed JToken Load and Parse methods not checking for incomplete content, but your're stuck on 35r8 because that's the last version that supports the compact framework. In that case, the following static helper methods will check that the start and end depths match:

public static class Json35Extensions
{
    public static JObject ParseObject(string json)
    {
        using (var reader = new JsonTextReader(new StringReader(json)))
        {
            var startDepth = reader.Depth;
            var obj = JObject.Load(reader);
            if (startDepth != reader.Depth)
                throw new JsonSerializationException("unclosed json found");
            return obj;
        }
    }

    public static JArray ParseArray(string json)
    {
        using (var reader = new JsonTextReader(new StringReader(json)))
        {
            var startDepth = reader.Depth;
            var obj = JArray.Load(reader);
            if (startDepth != reader.Depth)
                throw new JsonSerializationException("unclosed json found");
            return obj;
        }
    }
}

使用Json.NET 3.5.8,对于您的两个测试JSON字符串,都将引发异常,但是如果我手动修复JSON,也不会有异常. (请注意-我测试了ParseObject版本,但尚未测试ParseArray版本.)

Using Json.NET 3.5.8, for both your test JSON strings, the exception gets thrown, but if I fix your JSON manually there is no exception. (Note - I tested the ParseObject version but I haven't tested the ParseArray version.)

这篇关于JSON.net CF 3.5,验证JSON是否完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆