Question Is it possible to write json in c#

ScuffedDouleur

New member
Joined
Nov 13, 2022
Messages
4
Programming Experience
Beginner
I'm trying to create a c# framework and a necessity I need is the ability to write with json in C# and if I can how can I go about doing so.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
C# doesn't have an easy way to embed JSON within the C# code. You could use non-standard JSON and use single quotes where you would normally use double quotes within the JSON. Ex.
C#:
var json = @"
{
    'myArray' : [
        { 'key' : 'value' },
        { 'key' : 'value' )
    ]
}
";
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
A quick follow-up. C# 11 now has raw string literals that simplifies embedding JSON into C# code.

 

ScuffedDouleur

New member
Joined
Nov 13, 2022
Messages
4
Programming Experience
Beginner
A quick follow-up. C# 11 now has raw string literals that simplifies embedding JSON into C# code.

So your original post is invalid?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
If you are stuck with using .NET Framework, there's no (current) way for you to use C# 11, You'll be at C# 7.3. To use C# 11, you need to be using .NET 7.x.

 

cjard

Well-known member
Joined
Jan 25, 2012
Messages
168
Programming Experience
10+
Serialize an anonymous type?

C#:
var j = JsonConvert.SerializeObject(new {
  myInt = 1,
  myString = "a",
  myObject = new {
    anotherInt = 1
  },
  myArray = new[]{ 1,2,3 },
  myObjArray = new[]{ new { a = 1}, new { a = 2 } }
});

The syntax is nearly the same as json but you get all that OO goodness, and also you get nice easy value insertion..

C#:
var j = JsonConvert.SerializeObject(new {
  myObject = my.SomeMethodThatReturnsAnObject()
});

without having to have carp like:

C#:
var myObj = my.SomeMethodThatReturnsAnObject();

var j = JsonConvert.SerializeObject(new {
  myObject = new { 
    a = myObj.A,
    b = myObj.Bee
  }
});

or worse:


C#:
var myObj = my.SomeMethodThatReturnsAnObject();

var j = @"{
    myObject: {  
    a: " + myObj.A + @"
    b:" + myObj.Bee + @"
  }
}";


---


You should really be handing the job of serializing off to a serializer anyway, rather than rolling your own string-baked manual json
 
Last edited:

cjard

Well-known member
Joined
Jan 25, 2012
Messages
168
Programming Experience
10+
C# doesn't have an easy way to embed JSON within the C# code
And for good reason ;) but you could always alternatively engage in some replacement of the ' to " to make it valid again, or embed the string in something that understands strings (text file, embedded resource type thinger) to put straight json in there..

Putting it in code is a bit like going "oh, I don't store my PNGs on disk as files, I base64 them and paste them into my sourcecode!"
And there's another option; b64 it and paste it in inside a snipper that decodes it..

"Dirty hacks I want you, dirty hacks I need you (oh whoa)" :D
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
Serialization doesn't allow you to put in non-standard JSON comments.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
Very true, but imagine spitting out something as complex as the Visual Code settings JSON file without comments embedded in it. The supposedly human readable JSON file becomes about as unreadable as supposedly human readable XML. :)
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
Slightly tangent topic: Embedding XML into C# code.

C# doesn't have an easy way to embed JSON within the C# code.

And for good reason ;)

And one wonders who at Microsoft came up with CAML for SharePoint (about 5 years before there was LINQ-to-XML) where one is basically forced to build XML templates for the CAML query, and just fill in the blanks. Ex.
XML:
<Query>
  <Where>
    <Geq>
      <FieldRef Name="Field Name"/>
      <Value Type="DateTime">
        <Today/>
      </Value>
    </Geq>
  </Where>
  <OrderBy>
    <FieldRef Name="Field Name"/>
  </OrderBy>
</Query>
 
Last edited:
Top Bottom