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

Mostafa

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 <T> T toObj(byte[] bytes, Class<T> responseType) {
    
}
oleg.cherednik

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("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;
    }
}

Here you can get more info:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting String to Byte Array and vice versa

Put byte array to JSON and vice versa

Overhead of converting from []byte to string and vice-versa

byte to string and vice versa

Converting from Instant to XMLGregorianCalendar and vice-versa without losing precision

Converting XDocument to XmlDocument and vice versa

BitmapImage to byte array and vice versa

XML To JSON and Vice Versa

Converting JSON between string and byte[] with GSON

Which optimisations does the Go 1.6 compiler apply when converting between []byte and string or vice versa?

Converting the result of document.querySelector or document.querySelectorAll to a jquery object or vice versa without a reselection?

Cast without unsafe between struct and Span<byte> and vice versa in .C# and NET 5

R - Converting formulae to character strings and vice versa

Converting Secret Key into a String and Vice Versa

"Converting" Numpy arrays to Matlab and vice versa

"Converting" Numpy arrays to Matlab and vice versa

Converting Strings to encryption keys and vice versa java

Converting pandas dataframe to dict and vice versa

Converting a population on a grid to coordinates, and vice versa

Converting QString to Ascii value & Vice Versa in Qt

Converting an Armadillo Matrix to an Eigen MatriXd and vice versa

Converting Double to Float and vice versa manually

Converting string to integer and vice versa in Python

Converting base 6 to decimal and vice versa in Python?

Converting char to binary strings and vice-versa

Convert a byte array to integer in Java and vice versa

Convert file to byte array and vice versa

How to convert byte array to string and vice versa?

How to convert a float into a byte array and vice versa?

TOP Ranking

HotTag

Archive