ralfstx / minimal-json

A fast and small JSON parser and writer for Java
MIT License
736 stars 185 forks source link

How to String convert Object #72

Open dylan-tao opened 8 years ago

dylan-tao commented 8 years ago

how to String json convert oo Object (ps: User) ,String json convert Array (ps: List) and oo Object or Array convert String json, please tell me , thanks , i can not find about the code on the api and example.

ralfstx commented 8 years ago

Sorry, I can't understand your question. What is the input you have and what is the output you expect?

dylan-tao commented 8 years ago

Java class:

`public class User {

private Integer userId;

private String userName;

private String password;

public Integer getUserId() {
    return userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}`

JSON String:

{ "userId": 12, "userName": "dylan", "password": "555c98988d4f86ae944217e3032f9044" }

How to JSON String convert Object or ArrayList (ps : List) ?

ralfstx commented 8 years ago
new JsonObject()
  .add("userId", user.getUserId())
  .add("userName", user.getUserName())
  .add("password", user.getPassword())
  .toString();
dylan-tao commented 8 years ago

thanks! Is so simple, If there is such a API would be good. ps: new JsonObject(User user).toString(); or new JsonObject(String jsonStr).toObject(User.class); Hard coding is not a good solution for Java. What do you think?

GSON example

Object to JSON String:

ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writeValueAsString(JsonString);

JSON String to Object:

ObjectMapper objectMapper = new ObjectMapper(); objectMapper.readValue(JsonObject, User.class);

arcticicestudio commented 8 years ago

You are comparing two completely different mechanisms. The idea of this library is to not be an object mapper! Libraries like GSON and Jackson are using reflection to generate JSON from objects which is usefull for large enterprise projects and especially Jackson is good for Spring projects. But reflection also produces a lot of overhead and is not supported on every platform and some old JDK versions. When using reflection there's also a chance of unwanted JSON results since you only have a few options to control the way it creates the JSON string. By the way, your example looks like Jackson, not GSON :stuck_out_tongue:

minimal-json is a bare-bone library which gives you full control over the serialization. This library ensures that only valid JSON strings and needed data are processed while Jackson/GSON may also parse everything, even if it can't map the data.

Hard coding is not a good solution [...]

I think you misunderstand the term "Hard coding". If you try to say that the lines of code are a little bit higher you should use a JVM profiler to see which high amount of overhead Jackson/GSON produces when only used for a simple class like the one you provided. Compare it to minimal-json and you'll see one of the advantages of this library :smile:

dylan-tao commented 8 years ago

yeah~ A few days ago,I attention this class library,i want refer to my framework, because it is so faster and small. o(∩_∩)o