1

I'm calling a RESTful api which gives me the following response:

{
    "asks": [
        [
            "219.82",
            "2.19"
        ],
        [
            "219.83",
            "6.05"
        ]
    ],
    "bids": [
        [
            "219.40",
            "17.46"
        ],
        [
            "219.13",
            "53.93"
        ]
    ]
}

My data object implementation looks now like this:

public class OrderBook implements Serializable {

    /**
     * list containing the price and amount of all bid orders
     */
    private String[][] bids;

    /**
     * list containing the price and amount
     * of all ask orders
     */
    private String[][] asks;
}

How can I replace this String[][] with a data object and ensure that GSON can still parse it?

The api is external and I cant there change anything. The result is an array of an array with two properties. Instead of parsing this in java with GSON in a String[][], I'd like to have a Asks and Bids class. The Asks class should contain an array of the object Ask and the object Ask should have two variables: which are value and amount as String. This is what I'd like to have in my java code.

botscripter
  • 805
  • 2
  • 11
  • 21

1 Answers1

2

If I understand you correctly, you want to change the type of bids from String[][] to Object.

If you do that, you would have to look into creating a custom deserializer for that class. See here for details.

But then: don't do that. Why do you want give up on type safety? The quality of your Java source code does not improve when you willingly reduce the type information.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Hi GhostCat. Thanks for your help. I want to have an Ask, Bid, and Entry Object as minimum but I can't change the api. Do you now what I mean? – botscripter Nov 17 '17 at 14:58
  • No sorry, I don't get your point. It is **better** when your java code understands that bids is an array array of strings. Everything is an object anyway. – GhostCat Nov 17 '17 at 15:04
  • Okay i try to explain it better: The api is external and I cant there change anything. The result is An array of an array with two properties. Instead of parsing this in java with GSON in a String[][], I'd like to Have a Asks and Bids class. The Asks class should contain an Array of the Object Ask and the Object Ask should have two variables: which are value and amount as String. This is what I'd like to have in my java code. – botscripter Nov 17 '17 at 15:06
  • Dont put more information into comments. Instead enhance your question text! – GhostCat Nov 17 '17 at 15:22