0

I have the following class Record:

    public class Record
{

    public string Station;
    public string UserName;
    public int EvtActive;
    public string EvtTime;
    public string EvtTimeString;
    public string LocCode;
    public string LastLoop;
    public int CompLvl;
    public int RecordID;
    public string ConnectTime;
    public string Notes;
    public string Color;



    public Record(string a, string b, int c, string d, string e, string f, string g, int h, int i, string j, string k)
    {

        this.Station = a;
        this.UserName = b;
        this.EvtActive = c;
        this.EvtTime = d;
        this.EvtTimeString = e;
        this.LocCode = f;
        this.LastLoop = g;
        this.CompLvl = h;
        this.RecordID = i;
        this.ConnectTime = j;
        this.Notes = k;
        this.Color = get_color(this.LocCode);
        SwordsServer.record_list.Add(this);

        Console.WriteLine("Creating Record");
        Console.WriteLine(this.Station);
        Console.WriteLine(this.UserName);
        Console.WriteLine(this.LocCode);

    } 

When I do something like:

Record r = new Record("Support-28", "TEST USER", 0, "", "", "TEST CODE", "", 0, 0, "", "")

I will see r.Station, r.UserName, and r.LocCode print out in Console.

Though, when I use JSON like this:

Record json_record = JsonConvert.DeserializeObject<Record>(System.Text.Encoding.ASCII.GetString(byte_slice))

I see nothing but

Creating Record

in my printout.

What's even more confusing is that if I add:

Console.WriteLine(json_record.LocCode)

immediately under the JsonConvert line, I do actually get the appropriate print out. Yet the get_color function gets "" passed into it.

This seems as if using JSON to create an object does not function the way I need it to. What am I missing?

1
  • do you have other constructor than this? Commented Sep 27, 2016 at 16:09

2 Answers 2

4

Well, usually JSON deserialization even requires a parameterless constructor, so I'm astonished this works at all.

Normally this would work as all the properties (or better: instance variables) you have are public. The usual way this is done is:

  1. parameterless constructor is called
  2. properties are set using reflection

I'd suggest you do the following:

  1. Add a parameterless constructor that does the "real" work and have your existing constructor call that to add the object to the list
  2. Change the instance variables to real properties and add getters/setters that perform stuff when the property is set
  3. For all other stuff you can't handle using the above two, add code after JSON deserialization that initializes the rest
Sign up to request clarification or add additional context in comments.

5 Comments

Could you give me an example? I've never heard of "reflection" in this setting.
Well when the JSON deserializer finds an element named Station it looks via reflection if there is a public property or variable named Station in the class you specified and sets this member to the value.
@MrDysprosium please google for reflection c#. This is a wide topic.
@ThorstenDittmar Your answer seems to be what I need, but I'm having trouble understanding how to implement. After I JSON deserialize, do I just call different methods within the Record class to help build the rest "manually"?
You could do that, you could also create extension methods, you should also use the property setters for some stuff. It is basically up to you. You can can even do stuff in the parameterless constructor as long as it does not rely on other stuff to be initialized.
0

Below is a modified version of your app, it should work as expected. By the way, you should not reference the SwordServer class directly, instead you can refactor it using dependency injection or IoC container; also please name your variables in a meaningful way. Hope this will help.

static void Main(string[] args)
{            
    var r = new Record("Support-28", "TEST USER", 0, "", "", "TEST CODE", "", 0, 0, "", "");
    string jsonStr = JsonConvert.SerializeObject(r);

    //var json_record = JsonConvert.DeserializeObject<Record>(System.Text.Encoding.ASCII.GetString(byte_slice));
    var json_record = JsonConvert.DeserializeObject<Record>(jsonStr);

    Console.ReadLine();
}

public class Record
{
    public string Station;
    public string UserName;
    public int EvtActive;
    public string EvtTime;
    public string EvtTimeString;
    public string LocCode;
    public string LastLoop;
    public int CompLvl;
    public int RecordID;
    public string ConnectTime;
    public string Notes;
    public string Color;

    public Record(string station, string userName, int evtActive, string evtTime, string evtTimeString, string locCode, string lastLoop, int compLvl, int recordIDi, string connectTime, string notes)
    {
        this.Station = station;
        this.UserName = userName;
        this.EvtActive = evtActive;
        this.EvtTime = evtTime;
        this.EvtTimeString = evtTimeString;
        this.LocCode = locCode;
        this.LastLoop = lastLoop;
        this.CompLvl = compLvl;
        this.RecordID = recordIDi;
        this.ConnectTime = connectTime;
        this.Notes = notes;
        this.Color = "red";

        SwordsServer.Records.Add(this);

        Console.WriteLine("Creating Record");
        Console.WriteLine(this.Station);
        Console.WriteLine(this.UserName);
        Console.WriteLine(this.LocCode);
    }
}

public static class SwordsServer
{
    static SwordsServer()
    {
        Records = new List<Record>();
    }

    public static List<Record> Records { get; set;}
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.