0

I have this class for building strings:

public class WebSocketMessageBuilder
{
    private readonly string _eventName;
    private readonly Dictionary<string, string> _eventData;

    public WebSocketMessageBuilder(string eventName)
    {
        _eventName = eventName;
    }

    public void AddData(string key, string value) => _eventData[key] = value;

    public string ToJson()
    {
        return @"";
    }
}

I would like to produce JSON strings like:

{
    "event_name": "testing1",
    "event_data": {
        "key1": "value1",
        "key2": "value2"
    }
}

This string could be created like:

var wsMessage = new WebSocketMessageBuilder("testing1");
wsMessage.AddData("key1", "value2");
wsMessage.AddData("key1", "value2");

Console.WriteLine(wsMessage.ToJson());

How would I finish off the ToJson method? The documentation showed me some cool stuff, but nothing that helped me do this?

I have tried this, but it seems to just give me my class name back?

return new JObject {["event_name"] = _eventName, ["event_data"] = JsonConvert.SerializeObject(_eventData)}.ToString();
1
  • my answer has been updated and giving what you expect Commented Dec 13, 2020 at 19:36

2 Answers 2

1

You can use the following way to serialize the object,

using Newtonsoft.Json;  // This goes with the other using statements on top.

public string ToJson()
{
    return JsonConvert.SerializeObject(this, Formatting.Indented);
}

this is the reference to the object itself. Formatting.Indented will produce the serialized version with the proper indentation.

To make it work with the private properties, use the attribute [JsonProperty] above the private properties. See documentation here

Excerpt:

By default a type's properties are serialized in opt-out mode. What that means is that all public fields and properties with getters are automatically serialized to JSON, and fields and properties that shouldn't be serialized are opted-out by placing JsonIgnoreAttribute on them. To serialize private members, the JsonPropertyAttribute can be placed on private fields and properties

public class test
{
    [JsonProperty]
    private string prop1 { get; set; }
    [JsonProperty("property_name")]
    private string prop2 { get; set; }
}

Note: When I ran your code, it produced errors with the dictionary because it was not initizlied, in your constructor, you should add the initialization as well,

public WebSocketMessageBuilder(string eventName)
{
    _eventName = eventName;
    _eventData = new Dictionary<string, string>(); // this will help with NRE errors.
}
Sign up to request clarification or add additional context in comments.

5 Comments

I already tried it this way, it does not use my property names the documentation doesn't show any way to construct your own JObject? Please see my update to my question.
@LiamSavage See my note about [JsonProperty] attribute. I tested this and it works
this forces me to name my properties the same way I want them displayed in JSON, which breaks my convention.
@LiamSavage you can add the name of the property you want it serialized as. [JsonProperty("property_name_to_serialize_as")]. Try that.
@LiamSavage, does this answer your question?
0
//using System.Text.Json;

public string ToJson()
{
    return JsonSerializer.Serialize(this);
}

---- Update : ----

With the following code, I am getting exactly what you expect.

Note that Fields are converted to properties with the private set (this is important) and dictionary added to the Constructor.

public class WebSocketMessageBuilder
{
    public string event_name { get; private set; }
    public Dictionary<string, string> event_data { get; private set; }

    public WebSocketMessageBuilder(string eventName, Dictionary<string, string> eventData)
    {
        event_name = eventName;
        event_data = eventData;
    }

    public string ToJson()
    {
        return JsonSerializer.Serialize(this);
    }
}

How to Call it?! :

    var eventData = new Dictionary<string, string>();
    eventData.Add("key1", "value1");
    eventData.Add("key2", "value2");

    var wsMessage = new WebSocketMessageBuilder("testing1", eventData);

    var jsonResult = wsMessage.ToJson();

Result I am getting

{"event_name":"testing1","event_data":{"key1":"value1","key2":"value2"}}

4 Comments

I already tried it this way, it does not use my property names the documentation doesn't show any way to construct your own JObject? Please see my update to my question.
@Liam Savage, If you still seek a solution let me know so I do some tests and try.
I am still seeking an answer.
@Liam Savage , my answer has been updated and giving what you expect, check it please.

Your Answer

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