-2

I'm trying to serialize a json string into an object. I'm forced to use .net version 4.6.2 so I can't use the tools I would normally use. I can't either user async because of the framework I'm working in.

I have come up with this method and it's supposed to serialize the json string into the object using Newtonsoft.Json:

private static void Serialize(object obj, string json)
{
    var serializer = new JsonSerializer();
    using (var sw = new StreamWriter(json))

    using (JsonWriter writer = new JsonTextWriter(sw))
    {
        serializer.Serialize(writer, obj);
    }
}

I have this exception:

System.IO.PathTooLongException: 'The path '{...}' is too long, or a component of the specified path is too long.'

The json holds 10.000 products so it's going to be long.

How can this be overcome?

Adding:

<?xml version="1.0" encoding="utf-8" ?>
<runtime>
    <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false" />
</runtime>

to App.config doesn't change anything.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Kresten
  • 810
  • 13
  • 36
  • 1
    it's the path that's too long, not json. – Nino Nov 10 '22 at 08:59
  • How does the `JSON` string look? Can we see what you're dealing with? What I got from here is that you have a single string that has `10,000` products... and you want to `serialize` into an object that exists somewhere in your code. – ThisQRequiresASpecialist Nov 10 '22 at 08:59
  • @Nino how can a path be too long? – ThisQRequiresASpecialist Nov 10 '22 at 09:00
  • 2
    Does `json` contain a file path or a JSON blob? I suspect you're using this API completely incorrectly – ScottishTapWater Nov 10 '22 at 09:10
  • 2
    You can serialize an object to json string, or deserialize a json string to an object. But _"I'm trying to serialize a json string into an object. "_ What does this mean? – shingo Nov 10 '22 at 09:24
  • 1
    @Nino: The problem is that the OP is trying to use JSON *as* a path... – Jon Skeet Nov 10 '22 at 09:37
  • 1
    Fundamentally it's unclear what you're trying to do. Unless you're trying to write to a file, using the `StreamWriter(string)` is *not* what you want. However, until we know what you're trying to do, it'll be very hard to help you do the right thing. – Jon Skeet Nov 10 '22 at 09:38

1 Answers1

1

From the official documentation:

Apps that target the .NET Framework 4.6.1 and earlier versions Long paths are disabled by default, and the legacy behavior is maintained. The runtime throws a PathTooLongException whenever a path exceeds 260 characters.

If this is undesirable, apps that target the .NET Framework 4.6.1 and earlier but run under the .NET Framework 4.6.2 can enable long path support by including the following setting in the element of the application configuration file:

<runtime>   
   <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false" />   
</runtime>
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Rajeesh Madambat
  • 3,231
  • 1
  • 11
  • 18
  • Damm I didnt even realise that was a thing. Nice One! – ThisQRequiresASpecialist Nov 10 '22 at 09:07
  • That didn't solve it. Should I do anything but add the commend to App.config ? – Kresten Nov 10 '22 at 09:22
  • This isn't the problem, I believe. The problem is that the OP is specifying a JSON string to the constructor of `StreamWriter`, which expects a path. The OP hasn't actually been clear about what they're trying to do, but I think the problem is how they're using `StreamWriter` to start with. – Jon Skeet Nov 10 '22 at 09:37