0

Using C# I've modeled a JSON message this way:

public class MessageBase<T>
{
    public MessageBase() { this.message = new List<T>(); }
    public string type { get; set; }
    public string id { get; set; }
    public List<T> message { get; set; }
}

public class LogonRequest
{
    public string userName { get; set; }
    public string password { get; set; }
}

MessageBase<LogonRequest> logon = new MessageBase<LogonRequest>()
{
    id = Guid.NewGuid().ToString(),
    type = "Logon"
};

logon.message.Add(new LogonRequest() {userName = "user", password = "123"});

var message = (new JavaScriptSerializer()).Serialize(logon);

I'm a JavaScript beginner and trying to do something similar as my C# code but this is the best I've come up with so far. Is there a more standard way to define this message structure and reuse it using JavaScript?

var messageObj = {id: 123456789, 
                  type: 'Logon',
                  message:[{username:'user', password:'123' }]
                 };

var message = JSON.stringify(messageObj);    
  • You could make a class in JS for the Message object, but you really don't need to. I'd say a simple object like what you've got is all that you'll really need for this. Something like that is fairly standard. – neilsimp1 Sep 26 '17 at 15:17
  • 3
    The way you're doing it is the standard way. – Robert Harvey Sep 26 '17 at 15:30
  • It will not get better than this. Maybe you are interested in Typescript: typescriptlang.org/play – Jelle Sep 27 '17 at 6:46
0

You can get some of the same behavior from Javascript, with the caveat that Javascript is not a typed language, which reduces your options of modeling things.

One concept that makes your C# models MessageBase<T> and LogonRequest possible is the concept of classes which is built into the language. Unlike C#, vanilla Javascript does not have a concept of class, and even in ES6 with the class keyword, it pays to understand the semantic differences between Javascript's prototype inheritance and the class of a first-class object-oriented language.

First, a syntax example of those models using pre-ES6 Javascript:

function Message(id, type, list) {
  this.id = id;
  this.type = type;
  if('undefined' === typeof(list)) {
    this.message = [];
  } else { this.message = list; }
}

function LogonRequest(userName, password) {
  this.userName = userName;
  this.password = password;
}

var logonRequest = new LogonRequest('user', '123');
var messageObj = new Message(123456789, 'Logon', logonRequest);
var message = JSON.stringify(messageObj);

In the above example, the functions LogonRequest and Message are called the "the constructor" of those classes, and the this inside those functions is the this context that is in place where the function is called (check out a relevant explanation for more info about how this works). This makes it important to use the new keyword, as is done on the following lines, which creates a new object context to which the this applies.

ES6 improves this syntax somewhat, giving us the following equivalent form:

class Message {
  constructor(id, type, list) {
    this.id = id;
    this.type = type;
    if('undefined' === typeof(list)) {
      this.message = [];
    } else { this.message = list; }
  }
}

But its use and inner-workings are exactly the same as above.

It is worth noting that some developers do not like this aspect of Javascript, and prefer a more strongly-typed syntactic expression as C# provides. This is what accounts for the advent of a number of languages that transpile to Javascript, most notably Typescript, which allows a syntax very similar in semantics to your C# example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.