Dynamically Rendering a JSON String to a Table in C# - c#

I need help mapping a complex C# object to a table dynamically, without manually defining a model. I'm using ASP.NET 4.5.
So I am using JSON.NET to serialize my complex C# object into a JSON object with
string json = JsonConvert.SerializeObject(myObject, Formatting.Indented);
This object is being read in dynamically from an external API. It includes complex attributes, like dictionaries, as well as standard strings, integers, etc. I input in a key and it returns the C# object.
I can't manually map the object to a model, since the object does change frequently with development, and needs to be extensible despite changes in the C# object. What is the best way to map this JSON object to a table? Essentially need to be able to render the JSON serialized from the object into a readable table with ASP.NET.
Thanks so much!

Related

Dynamically creating C# Dictionary<Cottle.Value, Cottle.Value> from json using System.Text.JSON without foreach

I’m trying to create an object of Dictionary<Cottle.Value, Cottle.Value> (Cottle is a templating engine that uses it’s own type for storing keys and values to replace {key} text with values in a template) from a json string that doesn’t have nested values so that each and any pair of json key/values gets converted to a dictionary keyvaluepair…but the best I’ve managed to do so far is create a Dictionary<string, string> thus leaving me with the problem of creating Cottle.Value from string which can actually be done by calling Value.FromString(string) but doing that requires going through a foreach loop, so I’m just wondering is there a more direct way to do that? TIA
P.S.
Unfortunately, I’m not using Newtonsoft to create a custom converter and may have missed the way to the same with System.Text.JSON so that could be an easiest solution if still possible but seems too complicated for a simple thing I’m trying to do…adding it to a webapi.

Does "NullValueHandling.Ignore" only works with POCOs?

I have api calls that return expando object & dynamic objects. Even though I have NullValueHandling set to IgnoreNulls, nulls are still sent back in the resulting json. I have tested it with my other calls that return POCO objects, and they function correctly (omitting null value fields). Is there a way around this?
I was thinking of trying to convert the expando\dynamic objects to something that that the serializer could process the same way it does POCO object results, but I don't know what that would be.
I tried manually serializing the object and then de-serializing it into a JSONArrayObject before it gets to the final Serialization in the MVC middleware, but that didn't work.
Also, I can't just create a POCO for these objects, because they are the result of "Data Shaping" i.e. the user sends in the fields they want to receive in the object, and then we take the resulting POCO, and turn it into an expando object with only the fields they requested.

How do I use a JSON string as variable data for a Liquid.NET template?

I'm looking for the best way to use a JSON string as the variable data for a Liquid.NET template. I don't know the structure of the JSON so I am unable to deserialize it to a POCO before using it.
I attempted to use the .ToLiquid() extension method on a JSON.Net JObject but it throws a memory exception.
I may need to convert the JSON to a LiquidHash before merging it with the template. Is there a utility that provides this or do I need to iterate over all the nodes myself?
https://github.com/mikebridge/Liquid.NET
You currently need to generate a LiquidHash manually.

Deserializing Json string to C# Class object or DataSet. Which is more efficient?

I am using JSON.Net to deserialize a JSON string to object. My JSON string consists of huge data which can be loaded onto an array or dataset. Could someone please let me know which of the below appraoch is more efficient for the same.
var CSharpClassObject = JsonConvert.DeserializeObject<CSharpClass[]>(jsonString);
and
var dataSetObject = JsonConvert.DeserializeObject<DataSet>(jsonString);
Datasets are generally inefficient due to the memory they require, as well as events, etc. They are really suited to RAD (Rapid Application Development) and not too much else. For your purposes (and probably most others) I would certainly suggest using a custom type.

How to explicitly call the ASP.NET json serializer

So I have a small asp.net app which returns Json objects that are serialized from C# objects. If I just create a function:
[HttpGet(getTheObj)]
public SomeObj GetTheObject()
{
return new SomeObj() { SomeProperty = 1 };
}
Then it works fine and I can do an HttpRequest for the Json object. However I also want to save some these serialized objects into a database for later use. So I'm wondering, can I explicitly call the Json serializer? I understand that several different serializers can be used with ASP.NET, how do I figure out which one I am using (I didn't create the project).
string json = JsonConvert.SerializeObject(_data.ToArray());
you can save this in database. you can again retrive this object from database and deserialize this object.

Categories

Resources