Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用JsonTextReader或任何其他替代方法从json中读取特定对象作为流_C#_Json_Stream_Json.net_Large Data - Fatal编程技术网

C# 使用JsonTextReader或任何其他替代方法从json中读取特定对象作为流

C# 使用JsonTextReader或任何其他替代方法从json中读取特定对象作为流,c#,json,stream,json.net,large-data,C#,Json,Stream,Json.net,Large Data,必须将大文件作为JSON的一部分从HTTP Post读取到Web API。 As文件本身是JSON的一部分。 例如: 我只需要查找附件对象,但这里的“数据”包含一个大文件。可以是csv、pdf、JSON或图像,最重要的是大小大于1GB 根据分析,我们可以通过这些包装器读取这些信息 public class AttachmentParser : JsonParser<Attachment> { public AttachmentParser(Stre

必须将大文件作为JSON的一部分从HTTP Post读取到Web API。 As文件本身是JSON的一部分。 例如:

我只需要查找附件对象,但这里的“数据”包含一个大文件。可以是csv、pdf、JSON或图像,最重要的是大小大于1GB

根据分析,我们可以通过这些包装器读取这些信息

public class AttachmentParser 
             : JsonParser<Attachment>
{
    public AttachmentParser(Stream json, string jsonPropertyName)
        : base(json, jsonPropertyName)
    {
        Parse();
    }

    public override void Build(Attachment parsable, JsonTextReader reader)
    {
        if (reader.Value.Equals("extension"))
        {
            reader.Read();
            parsable.Extention = (string)reader.Value;
        }
        else if (reader.Value.Equals("data"))
        {
            reader.Read();
            parsable.Data = reader.Value;
        }
    }

    protected override bool IsBuilt(Attachment parsable, JsonTextReader reader)
    {
        var isBuilt = parsable.Extention != null && parsable.Data != null;
        return isBuilt || base.IsBuilt(parsable, reader);
    }

    private static MemoryStream SerializeToStream(object o)
    {
        MemoryStream stream = new MemoryStream();
        IFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, o);
        return stream;
    }
}
公共类AttachmentParser
:JsonParser
{
公共AttachmentParser(流json,字符串jsonPropertyName)
:base(json、jsonPropertyName)
{
Parse();
}
公共覆盖无效生成(附件可解析,JsonTextReader读取器)
{
if(reader.Value.Equals(“扩展”))
{
reader.Read();
parsable.Extention=(string)reader.Value;
}
else if(reader.Value.Equals(“数据”))
{
reader.Read();
parsable.Data=reader.Value;
}
}
已构建受保护的覆盖布尔(附件可解析,JsonTextReader读取器)
{
var isbuild=parsable.extension!=null&&parsable.Data!=null;
返回isbuild | | base.isbuild(parsable,reader);
}
私有静态内存流SerializeToStream(对象o)
{
MemoryStream stream=新的MemoryStream();
IFormatter formatter=新的BinaryFormatter();
序列化(流,o);
回流;
}
}
Json解析器:

 public abstract class JsonParser<TParsable>
    where TParsable : class, new()
{
    private readonly Stream json;
    private readonly string jsonPropertyName;

    protected JsonParser(Stream json, string jsonPropertyName)
    {
        this.json = json;
        this.jsonPropertyName = jsonPropertyName;

        Result = new TParsable();
    }

    public TParsable Result { get; private set; }

    public abstract void Build(TParsable parsable, JsonTextReader reader);

    protected virtual bool IsBuilt(TParsable parsable, JsonTextReader reader)
    {
        return reader.TokenType.Equals(JsonToken.None);
    }

    protected void Parse()
    {
        using (var streamReader = new StreamReader(json))
        {
            using (var jsonReader = new JsonTextReader(streamReader))
            {
                do
                {
                    jsonReader.Read();
                    if (jsonReader.Value == null || !jsonReader.Value.Equals(jsonPropertyName))
                    {
                        continue;
                    }

                    var parsable = new TParsable();

                    do
                    {
                        jsonReader.r;
                    }
                    while (!jsonReader.TokenType.Equals(JsonToken.PropertyName) && !jsonReader.TokenType.Equals(JsonToken.None));

                    do
                    {
                        Build(parsable, jsonReader);
                        jsonReader.Read();
                    }
                    while (!IsBuilt(parsable, jsonReader));

                    Result = parsable;
                }
                while (!jsonReader.TokenType.Equals(JsonToken.None));
            }
        }
    }
}
公共抽象类JsonParser
其中TParsable:class,new()
{
私有只读流json;
私有只读字符串jsonPropertyName;
受保护的JsonParser(流json、字符串jsonPropertyName)
{
this.json=json;
this.jsonPropertyName=jsonPropertyName;
结果=新的TParsable();
}
公共TParsable结果{get;private set;}
公共摘要无效构建(TParsable parsable,JsonTextReader reader);
已构建受保护的虚拟布尔(TParsable parsable、JsonTextReader阅读器)
{
返回reader.TokenType.Equals(JsonToken.None);
}
受保护的void Parse()
{
使用(var streamReader=newstreamreader(json))
{
使用(var jsonReader=newjsontextreader(streamReader))
{
做
{
jsonReader.Read();
if(jsonReader.Value==null | |!jsonReader.Value.Equals(jsonPropertyName))
{
继续;
}
var parsable=新的TParsable();
做
{
jsonReader.r;
}
而(!jsonReader.TokenType.Equals(JsonToken.PropertyName)和&!jsonReader.TokenType.Equals(JsonToken.None));
做
{
构建(parsable、jsonReader);
jsonReader.Read();
}
而(!isbuild(parsable,jsonReader));
结果=可分析;
}
而(!jsonReader.TokenType.Equals(JsonToken.None));
}
}
}
}
查询: 这里JSONTextReader有一个jsonReader.Read()方法,该方法不逐个元素读入,但默认情况下作为对象读取。这将在内存中,并在此操作期间增加更多内存负载。 Had查看了重载不包含ReadAsStream方法

有没有办法通过不同的库或同一个库以不同的方式将大数据作为流读取(尽管没有过载可用)

 public abstract class JsonParser<TParsable>
    where TParsable : class, new()
{
    private readonly Stream json;
    private readonly string jsonPropertyName;

    protected JsonParser(Stream json, string jsonPropertyName)
    {
        this.json = json;
        this.jsonPropertyName = jsonPropertyName;

        Result = new TParsable();
    }

    public TParsable Result { get; private set; }

    public abstract void Build(TParsable parsable, JsonTextReader reader);

    protected virtual bool IsBuilt(TParsable parsable, JsonTextReader reader)
    {
        return reader.TokenType.Equals(JsonToken.None);
    }

    protected void Parse()
    {
        using (var streamReader = new StreamReader(json))
        {
            using (var jsonReader = new JsonTextReader(streamReader))
            {
                do
                {
                    jsonReader.Read();
                    if (jsonReader.Value == null || !jsonReader.Value.Equals(jsonPropertyName))
                    {
                        continue;
                    }

                    var parsable = new TParsable();

                    do
                    {
                        jsonReader.r;
                    }
                    while (!jsonReader.TokenType.Equals(JsonToken.PropertyName) && !jsonReader.TokenType.Equals(JsonToken.None));

                    do
                    {
                        Build(parsable, jsonReader);
                        jsonReader.Read();
                    }
                    while (!IsBuilt(parsable, jsonReader));

                    Result = parsable;
                }
                while (!jsonReader.TokenType.Equals(JsonToken.None));
            }
        }
    }
}