0

I read large json file, change certain things and then write it back to disk:

using (var reader = new StreamReader(filePath))
        {
            var log = (JObject)JToken.ReadFrom(new JsonTextReader(reader));
            //log = UpdateOneLog(log);
            using (var writer = new StreamWriter(updateFilePath))
            {
                log.WriteTo(new JsonTextWriter(writer));
                writer.Close();
            }
            reader.Close();
        }

or even

JObject o1 = JObject.Parse(File.ReadAllText(inputFile));
File.WriteAllText(outputFile, o1.ToString());

Weird things happen for certain files and I believe it has something to do with file size. The date time should be startedDateTime":"2013-01-17T11:00:40.000-06:00" but it gets written to the file as startedDateTime":"2013-01-17T11:00:40-06:00" (note that fractions of the second "000" is missing). I even commented out my update logic as shown above. All I am doing is reading file and writing it back but the date gets garbled..

Am I doing something wrong?

-Stan

4

1 回答 1

0

由于我仍然不清楚的原因(这可能是一个错误),Json.Net 有时会错误地解析日期/时间的毫秒部分。因此,例如在字符串“2013-01-17T11:00:40.230-06:00”中,毫秒部分“230”被删除,字符串变为“2013-01-17T11:00:40-06:00”是无效的。我发现的解决方法是在保存时遍历所有令牌并将毫秒替换为某个值,如下所示。

    [TestMethod]
    public void LoadAndSave()
    {
        var directory = @"..\..\Files";
        var inputFile = Path.Combine(directory, "LargeFile.har");
        var outputFile = Path.Combine(directory, "LargeFileResult.har");

        if (File.Exists(outputFile))
            File.Delete(outputFile);

        StreamWriter sw = null;
        JsonTextWriter jTextWriter = null;
        StreamReader sr = null;
        JsonTextReader jTextReader = null;
        try
        {
            sw = new StreamWriter(outputFile);
            jTextWriter = new JsonTextWriter(sw);
            sr = new StreamReader(inputFile);
            jTextReader = new JsonTextReader(sr);
            while (jTextReader.Read())
            {
                var tokenType = jTextReader.TokenType;
                var tokenValue = jTextReader.Value;
                var tokenString = jTextReader.Value as string;
                switch (tokenType)
                {
                    case JsonToken.Boolean:
                    case JsonToken.Bytes:
                    case JsonToken.Float:
                    case JsonToken.Integer:
                    case JsonToken.String:
                          jTextWriter.WriteValue(tokenValue);
                        break;                        
                    case JsonToken.Comment:
                        jTextWriter.WriteComment(tokenString);
                        break;
                    case JsonToken.Date:
                        DateTime date = (DateTime)tokenValue;
                        DateTime dateWrite = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, 100, date.Kind);
                        jTextWriter.WriteValue(dateWrite);
                        break;
                    case JsonToken.EndArray:
                        jTextWriter.WriteEndArray();
                        break;
                    case JsonToken.EndConstructor:
                        jTextWriter.WriteEndConstructor();
                        break;
                    case JsonToken.EndObject:
                        jTextWriter.WriteEndObject();
                        break;                        
                    case JsonToken.None:                                
                        break;
                    case JsonToken.Null:
                        jTextWriter.WriteNull();
                        break;
                    case JsonToken.PropertyName:
                        jTextWriter.WritePropertyName(tokenString);
                        break;
                    case JsonToken.Raw:
                        jTextWriter.WriteRaw(tokenString);
                        break;
                    case JsonToken.StartArray:
                        jTextWriter.WriteStartArray();
                        break;
                    case JsonToken.StartConstructor:
                        jTextWriter.WriteStartConstructor(tokenString);
                        break;
                    case JsonToken.StartObject:
                        jTextWriter.WriteStartObject();
                        break;                        
                    case JsonToken.Undefined:
                        jTextWriter.WriteUndefined();
                        break;
                    default:
                        break;
                }                                            
            }
        }
        finally
        {
            jTextReader.Close();
            sr.Close();
            jTextWriter.Close();
            sw.Close();               
        }
于 2013-03-10T21:14:48.930 回答