2

I have a simple class like so:

public class MyClass
{
        public string String1;
        public string String2;

        public MyClass()
        {

        }

        public MyClass(string Json)
        {

        }
}

If the class is instantiated without a parameter, it just brings back an empty object.. However, when I pass a string of JSON to the constructor I would like to set all the properties equal to their value in the JSON.

I have done this outside of the class using JSON.NET by Newtonsoft, like this:

string JsonArray = Base64.Decode(HttpContext.Current.Request.QueryString["songinfo"]);
MyClass tmp = JsonConvert.DeserializeObject<MyClass>(JsonArray);
MyClass InstantiatedClass = tmp;

InstantiatedClass is now a fully populated object.. But, I would like to achieve this within the constructor itself, but I can't figure out a way to do it..

I've tried things like setting this = JsonConvert.DeserializeObject<MyClass>(Json), but of course, this is read-only.

I know I could do it by parsing the Json and setting each variable equal to it's appropriate JSON attribute value, but I'd like to do it this way because it's cleaner.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Dave
  • 25
  • 1
  • 4
  • Of course, if `MyClass` had been a `struct`, your approach with `this = JsonConvert.DeserializeObject(Json);` would have worked. – Jeppe Stig Nielsen Jan 27 '14 at 22:19
  • The only way to do it is to create a second instance inside the constructor and then use reflection to clone all the properties. The static factory methods mentioned in the answers are the next best thing compared to what you're asking and a better/cleaner solution even. – Mark Jan 27 '14 at 22:19
  • I don't have an answer to your question and it seems that you already have some but I wanted to correct your terminology. You don't "instantiate variables". The only thing that you can instantiate is a type, by creating an object of that type. What you want to do is "initialise" those variables, with an object that is the instantiation of a type. – jmcilhinney Jan 27 '14 at 22:19

3 Answers3

4

You can't do it from the constructor. You might consider a factory method within MyClass like so:

public static MyClass Deserialize(string json)
{
    return JsonConvert.DeserializeObject<MyClass>(json);
}

A constructor doesn't return anything, and as you stated this is read-only. Thus, doing it in the constructor isn't really possible without (as you stated) manually parsing the JSON and initializing the different variables within that instance.

The factory method is probably what you're looking for.

McAden
  • 13,714
  • 5
  • 37
  • 63
  • 3
    A note: If you take this approach, you might want to make the constructor `private` so that the object can only be constructed via the factory method. – Daniel Mann Jan 27 '14 at 22:18
3

You could do sth like this:

public class MyClass
{
    public string String1;
    public string String2;

    public MyClass()
    {

    }

    public static MyClass Instance(string json)
    {
        return new JsonConvert.DeserializeObject<MyClass>(json);
    }
}

But this is just some kind of a workaround on what you want to achieve. There is in fact no possibility to do this via constructor directly.

Paweł Bejger
  • 6,176
  • 21
  • 26
0
public MyClass(string Json)
{
    MyClass tmp = (MyClass) JsonConvert.DeserializeObject<MyClass>(Json);

    this.String1 = tmp.String1;
    this.String2 = tmp.String2;
}

Note, you have access to all private members of the MyClass inside MyClass methods.