将byte[]转换为JSON,反之亦然,不使用jackson或gson。

huangapple go评论69阅读模式
英文:

Converting byte[] to json and vice versa without jackson or gson

问题

我有一个使用Java 1.5构建的遗留应用程序,我需要在字节数组(byte[])和JSON之间进行转换,但我不能使用jackson或gson,因为它们适用于更高版本的java。
我有如下的方法,但我无法找到使用JSONObject实现的方法:

public <T> T toObj(byte[] bytes, Class<T> responseType) {

}
英文:

I have a legacy application which has been built with java 1.5, I need a conversion between byte[] and json but I cannot use jackson or gson, because they are in a higher version of java.
I have methods like these and I couldn't find a way to implement with JSONObject :

public &lt;T&gt; T toObj(byte[] bytes, Class&lt;T&gt; responseType) {
	
}

答案1

得分: 2

如果事情真的那么简单,那么 Jackson 或者 Gson 就不会诞生。

恐怕您必须为所有对象手动声明反序列化器。这不是火箭科学,但是需要花时间来完成。这是一个示例:

public static void main(String[] args) {
    Data data = new Data(11, 12);
    String json = toJson(data);
    System.out.println(json);

    byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
    Data res = toDataObj(bytes);
    System.out.println(res.a);
    System.out.println(res.b);
}

public static String toJson(Data data) {
    JSONObject jsonObj = new JSONObject();
    jsonObj.put("a", data.a);
    jsonObj.put("b", data.b);
    return jsonObj.toString();
}

public static Data toDataObj(byte[] bytesClass) {
    JSONObject jsonObject = new JSONObject(new String(bytesClass, StandardCharsets.UTF_8));
    Data data = new Data(0, 0);
    data.a = jsonObject.getInt("a");
    data.b = jsonObject.getInt("b");
    return data;
}

public static class Data {

    int a;
    int b;

    public Data(int a, int b) {
        this.a = a;
        this.b = b;
    }
}

这里您可以获取更多信息:

英文:

If it would be so easy, then Jackson or Gson was never be born.

I am affraid, that you have to declared deserializer for all of your objects manualy. This is not a rocket science, but it takes time to do it. This is an example:

public static void main(String[] args) {
    Data data = new Data(11, 12);
    String json = toJson(data);
    System.out.println(json);

    byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
    Data res = toDataObj(bytes);
    System.out.println(res.a);
    System.out.println(res.b);
}

public static String toJson(Data data) {
    JSONObject jsonObj = new JSONObject();
    jsonObj.put(&quot;a&quot;, data.a);
    jsonObj.put(&quot;b&quot;, data.b);
    return jsonObj.toString();
}


public static Data toDataObj(byte[] bytesClass) {
    JSONObject jsonObject = new JSONObject(new String(bytesClass, StandardCharsets.UTF_8));
    Data data = new Data(0, 0);
    data.a = jsonObject.getInt(&quot;a&quot;);
    data.b = jsonObject.getInt(&quot;b&quot;);
    return data;
}

public static class Data {

    int a;
    int b;

    public Data(int a, int b) {
        this.a = a;
        this.b = b;
    }
}

Here you can get more info:

huangapple
  • 本文由 发表于 2020年9月12日 12:56:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63856948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定