com.bybit.api.client.util.JSONParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bybit-java-api Show documentation
Show all versions of bybit-java-api Show documentation
The Official Java API connector for Bybit's HTTP and WebSocket APIs.
Dive into a plethora of functionalities:
- Market Data Retrieval
- Trade Execution
- Position Management
- Account and Asset Info Retrieval
- User and Upgrade Management
— Public Websocket Streaming
- Private Websocket Streaming
- Lending Institution and Client
- Broker Earning Data
The newest version!
package com.bybit.api.client.util;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public final class JSONParser {
private JSONParser() {
}
public static String getJSONStringValue(String json, String key) {
try {
JSONObject obj = new JSONObject(json);
return obj.getString(key);
} catch (JSONException e) {
throw new JSONException(String.format("[JSONParser] Failed to get \"%s\" from JSON object", key));
}
}
public static int getJSONIntValue(String json, String key) {
try {
JSONObject obj = new JSONObject(json);
return obj.getInt(key);
} catch (JSONException e) {
throw new JSONException(String.format("[JSONParser] Failed to get \"%s\" from JSON object", key));
}
}
public static String getJSONArray(ArrayList> symbols, String key) {
try {
JSONArray arr = new JSONArray(symbols);
return arr.toString();
} catch (JSONException e) {
throw new JSONException(String.format("[JSONParser] Failed to convert \"%s\" to JSON array", key));
}
}
public static String buildJSONString(Object id, String method, JSONObject parameters) {
try {
JSONObject json = new JSONObject();
json.put("req_id", id);
json.put("op", method);
json.put("args", parameters);
return json.toString();
} catch (JSONException e) {
throw new JSONException(String.format("[JSONParser] Failed to convert to JSON string"));
}
}
public static Map sortJSONObject(JSONObject parameters) {
LinkedList keys = new LinkedList<>(parameters.keySet());
Map sortedParams = new LinkedHashMap<>();
keys.stream().sorted().forEach(key -> sortedParams.put(key, parameters.get(key)));
return sortedParams;
}
public static JSONObject addKeyValue(JSONObject parameters, String key, Object value) {
if (parameters == null) {
parameters = new JSONObject();
}
return parameters.put(key, value);
}
public static Object pullValue(JSONObject parameters, String key) {
if (parameters == null) {
return null;
}
Object value = parameters.opt(key);
parameters.remove(key);
return value;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy