3

I'm trying to deal with JSON using standard C# libraries. I want to save some data from my program for further use. I decided to save them in JSON format. I got that every time a new array is added to the file. How do I make sure new elements are added to the old array instead? I can't understand what needs to be fixed.

Here is an example of serialization:

private static void writeDeviceInfoInLog(string ipNum, string portNum, string deviceNum)
{
    DeviceTCP device1 = new DeviceTCP(ipNum, portNum, deviceNum);
    DeviceTCP device2 = new DeviceTCP(ipNum, portNum, deviceNum);
    DeviceTCP[] devices = new DeviceTCP[] { device1, device2 };
    DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(DeviceTCP[]));

    using (FileStream fs = new FileStream("0.json", FileMode.Append))
    {
        jsonFormatter.WriteObject(fs, devices);
    }
}

Here is the output file:

[{
    "IpDevice":"1","NumberDevice":"3","PortDevice":"2"
},{
    "IpDevice":"1","NumberDevice":"3","PortDevice":"2"
}]
[{
    "IpDevice":"1","NumberDevice":"3","PortDevice":"2"
},{
    "IpDevice":"1","NumberDevice":"3","PortDevice":"2"
}]
4
  • 3
    FileMode.Append only appends to the file. it knows nothing about the JSON. you'll have to process the json in and then add it Commented Aug 11, 2018 at 14:09
  • jsonFormatter converts devices (which is an array) to a json string and then appends that string to a file. What you need, is to append that json data, and then write it to a file. You'll have to deserialize it back, merge it, and write back to file. e.g. you can check out Json.Net newtonsoft.com/json/help/html/MergeJson.htm Commented Aug 11, 2018 at 14:30
  • You need to read from file and deserialize the devices into a list. Add new items to the list. Serialize and write the whole array back into the file. Or you need to manipulate the file programmatically. Commented Aug 11, 2018 at 14:42
  • Set your file pointer to penultimate position, i.e. fs.Seek(-1, SeekOrigin.End), to overwrite the closing array character and write a comma character into the file stream. Then serialize your JSON not to the file stream, but first into a MemoryStream, afterwards set memory stream position to one to ignore the new start array character, and finally call MemoryStream.CopyTo to append it to the file. Commented Aug 11, 2018 at 14:59

1 Answer 1

1

You would have to read the old array from the file first, and then overwrite it with the new one. For example (if you are sure the file already exists and contains valid JSON):

DeviceTCP[] oldArray;
using (var fs = new FileStream("0.json", FileMode.Open))
{
    oldArray = (DeviceTCP[])jsonFormatter.ReadObject(fs);
}

using (var fs = new FileStream("0.json", FileMode.OpenOrCreate))
{
    jsonFormatter.WriteObject(fs, oldArray.Concat(devices).ToArray());
}

This might not be the most efficient approach, though, especially if you have to do this often. In this case, you might consider working with the array in memory and only write to the file once when done.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.