I see roughly 30'ish instances currently of the use ToStringStyle.SHORT_PREFIX_STYLE
used int the various toString()
override implementations I'd like to override with ToStringStyle.JSON_STYLE
.
example from CandlestickEvent.java
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
.append("eventType", eventType)
.append("eventTime", eventTime)
.append("symbol", symbol)
...
.append("isBarFinal", isBarFinal)
.toString();
}
I like to use various JSON parsers such as google's GSON library to read and write the toString()
outputs to file for logging or to be picked up by the database filereader. I might want to pass the JSON string via REST call to a web viewer as a JSON String. Also there are times when i want to pretty print them to console. i.e.
{
"symbol": "ETHUSDT",
"orderId": 12345678,
"clientOrderId": "smoD6joHgLUGlowNcZQcSf",
"transactTime": 1511234502823
}
If I currently pass the output in, its not in a standard JSON format and a malformed parser exception happens. I could create a data adapter to put it in a good JSON format but that fairly cumbersome for every object. I can just locally change the ToStringStyle.SHORT_PREFIX_STYLE
to ToStringStyle.JSON_STYLE
in every file that currently uses it but that would be difficult to maintain local dependency builds with updates upstream.
What I propose would be to have a non final, override-able constant in a Constants class. i.e.
public static ToStringStyle TO_STRING_BUILDER_STYLE = ToStringStyle.SHORT_PREFIX_STYLE;
where the previous toString()
example would refer to that constant. i.e.
@Override
public String toString() {
return new ToStringBuilder(this, TO_STRING_BUILDER_STYLE)
.append("eventType", eventType)
.append("eventTime", eventTime)
.append("symbol", symbol)
...
.append("isBarFinal", isBarFinal)
.toString();
}
where I could set it to ToStringStyle.JSON_STYLE
if needed and this would not break backward compatibility if someone is expecting the toString()
to be in SHORT_PREFIX_STYLE. Another option might be to start a user configuration file that sets this value or an initial programmatic binance-java-api configuration call where i could override the default setting for this value.
All in all i'd much rather use this library as a jar dependency without having to custom change and build it my self. Thought?
Just for fun, I'll provide my JSON pretty console print methods I have in a Utils class.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* Convert a JSON string to pretty print version
* @param jsonString
* @return
*/
public static String toPrettyJsonFormat(String jsonString) {
String prettyJson = jsonString;
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(jsonString);
if(jsonElement != null) {
JsonObject json = jsonElement.getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
prettyJson = gson.toJson(json);
}
return prettyJson;
}
/**
* Convert a JSON string to pretty print version.
* This assumes you expect the provided object to output a json
* string when the toString method is called
* @param obj
* @return
*/
public static String toPrettyJsonFormat(Object obj) {
return toPrettyJsonFormat(String.valueOf(obj));
}
/**
* Convert a JSON string to pretty print version and print to console.
* This assumes you expect the provided object to output a json
* string when the toString method is called
* @param obj
*/
public static void printPrettyJsonFormat(Object obj) {
System.out.println(toPrettyJsonFormat(obj));
}