【问题标题】:Node.js equivalent for GSON Json creation?Node.js 等效于 GSON Json 创建?
【发布时间】:2014-05-11 14:34:24
【问题描述】:

我在 servlet 中有一个 Java 类,它使用 GSON 将发布的 JSON 字符串呈现为 Java 对象。该方法的美妙之处在于,GSON 过滤掉了所有与类属性不匹配的 JSON 元素,因此我永远不会得到我不想处理的 JSON 内容。 servlet 的 doPost(简化)如下所示:

  protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    PrintWriter out = null;
    try {
        InputStream in = request.getInputStream();
        Demo d = Demo.load(in);
        in.close();
        response.setContentType("text/plain");
        response.setStatus(HttpServletResponse.SC_OK);
        out = response.getWriter();
        out.println(d.toJson);
    } catch (Exception e) {
        e.printStackTrace();
        out.println(e.getMessage());
    }
    out.close();
}

Demo 类(这是我需要在 common.js 或 node.js 中重新创建的类,如下所示:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Demo implements Serializable {

    public static Demo load(InputStream in) {
        Demo result = null;
        try {
            Gson gson = new GsonBuilder().create();
            result = gson.fromJson(new InputStreamReader(in), Demo.class);
        } catch (Exception e) {
           result = null;
        }   
        return result;
    }

    @TypeDiscriminator
    @JsonProperty("_id")
    private String              id                  = UUID.randomUUID().toString();
    private Date                created             = new Date();
    private String              color;
    private String              name;
    private String              taste;

    public String getColor() {
        return this.color;
    }

    public String getName() {
        return this.name;
    }

    public String getTaste() {
        return this.taste;
    }

    public Date getCreated() {
        return this.created;
    }

    public String getId() {
        return this.id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setTaste(String taste) {
        this.taste = taste;
    }


    public void setColor(String color) {
        this.color = color;
    }

    public String toJson() {
        GsonBuilder gb = new GsonBuilder();
        gb.setPrettyPrinting();
        gb.disableHtmlEscaping();
        Gson gson = gb.create();
        return gson.toJson(this);
    }
}

显然,我剥离了所有处理逻辑,servlet 只是回显 JSON,这不是应用程序所做的,但可以说明这一点。在那个例子中,我可以在 HTTP Post 中抛出几乎任何字符串,但我只能得到有效的 Demo 对象。

如何在 node.js 中做这样的事情?

【问题讨论】:

    标签: json node.js gson commonjs


    【解决方案1】:

    Node.js 是 Javascript,因此内置了对 json 的支持。您可以使用 JSON.parse 将字符串转换为 json 并包装在 try catch 块中。

    要仅包含选择属性,我知道节点中没有内置功能,除非您将 Mongodb 与 mongoose 一起使用,但您可以执行以下操作:拥有一个“类”,它是一个包含您想要的所有属性的对象,并且从解析的 json 对象中删除那些不在该“类”对象中的对象。

    var class = {x: null, y:null};
    for(var prop in object){ 
    if (!class.hasOwnProperty (prop)) {
    delete object [prop]
    }
    

    最好使用这个类作为对象,并暴露 parseJSON 函数来封装这个功能

    【讨论】:

    • 抱歉不清楚:GSON 过滤掉所有与类属性不匹配的 json。这就是问题的核心。如何将 json 过滤为我想要接受的内容?
    猜你喜欢
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2014-05-14
    相关资源
    最近更新 更多