R
R
Roman Rakzin2016-10-19 02:43:03
JSON
Roman Rakzin, 2016-10-19 02:43:03

How to generate Json in C#, if the structure is dynamic, without prescribing all possible options?

I am generating json using Newtonsoft.Json library;
The answer should be like

{Command:"CommandName", Data:{"Param1":1,"Param2:2"}}

There will always be a Command and Data structure, but the content is different. But the Data structure is also different.
How to organize the structure, so that if Command="command 1", then it would be generated according to one scenario, and if another, then in a different way?
For example:
{Command:"FilesList", Data:{"Files":["file1","file2","file3","file4"]}}
{Command:"UserInfo", Data:{"UserId":1, "UserName": "Roman" ....}}

That is, I will create the corresponding classes FilesList, UserInfo, but how to transfer the "undefined" class? or do method overloads? Can you somehow describe the base class, where there will be Command and Data, and somehow put the corresponding classes in Data and then generate json? This is how I create json.
string data = JsonConvert.SerializeObject(DataInfo, Formatting.Indented);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anatoliy Mikhailov, 2016-10-19
@yamaoto

You can create classes like this:

public class CommandResult
{
    public string Command {get;set;}
}
public class CommandResult<T>:CommandResult
{
    public T Data {get;set;}
}
public class FilesList
{
    public string[] Files {get;set;}
}
public class UserInfo
{
    ...
}

and use in WebApi
public class MyAwesomeController:ApiController
{

    public CommandResult<FilesList> GetFilesList(){
        ...
        return new CommandResult<FilesList> (){
            Command = "FilesList",
            Data = new FilesList(){Files = new []{"file1","file2","file3","file4"}}
        };
    }
 public CommandResult<UserInfo> GetUserInfo(){
...
 }
}

or with your own logic
public class MyAwesomeCommands
{

    private CommandResult<FilesList> _getFilesList(){
    ...
        return new CommandResult<FilesList> (){
            Command = "FilesList",
            Data = new FilesList(){Files = new []{"file1","file2","file3","file4"}}
        };
    }
    public string GetFilesList(){
        var data = getFilesList();
        return JsonConvert.SerializeObject(data);
    }
 public string GetUserInfo(){
...
 }
}

M
Michael, 2016-10-19
@Sing303

And dynamic does not suit?

dynamic filesListCommand = new
{
    Command = "FilesList",
    Data = new { Files = new[] { "file1", "file2", "file3", "file4" } }
};

dynamic userInfoCommand = new
{
    Command = "UserInfo",
    Data = new { UserId = 1, UserName = "Roman" }
};

string filesListCommandJson = JsonConvert.SerializeObject(filesListCommand);
string userInfoCommandJson = JsonConvert.SerializeObject(userInfoCommand);

dynamic filesListCommandParsed = JObject.Parse(filesListCommandJson);
dynamic userInfoCommandJsonParsed = JObject.Parse(userInfoCommandJson);

string file1Value = filesListCommandParsed.Data.Files[0]; // file1
string romanValue = userInfoCommandJsonParsed.Data.UserName; // Roman

R
Rou1997, 2016-10-19
@Rou1997

I don’t know how to do this with SerializeObject, I abandoned it due to poor performance and low flexibility, but no one bothers you using reflection to write a construction yourself that will indicate to the parser the class whose name is contained instring

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question