Last active
May 19, 2021 15:16
-
-
Save nacho4d/0a9b9f25d76017051334aaabc4f1d534 to your computer and use it in GitHub Desktop.
Because javax.json.Json is just too much hassle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company.project.util; | |
import java.io.StringReader; | |
import java.io.StringWriter; | |
import javax.json.Json; | |
import javax.json.JsonObject; | |
import javax.json.JsonReader; | |
import javax.json.JsonWriter; | |
public class JsonUtils { | |
static public String stringify(JsonStructure jsonObjectOrArray) { | |
StringWriter stWriter = new StringWriter(); | |
JsonWriter jsonWriter = Json.createWriter(stWriter); | |
jsonWriter.write(jsonObjectOrArray); | |
jsonWriter.close(); | |
String jsonString = stWriter.toString(); | |
return jsonString; | |
} | |
static public JsonObject parse(String jsonObjectString) { | |
JsonReader jsonReader = Json.createReader(new StringReader(jsonObjectString)); | |
JsonObject jsonObject = jsonReader.readObject(); | |
return jsonObject; | |
} | |
static public JsonArray parseArray(String jsonArrayString) { | |
JsonReader jsonReader = Json.createReader(new StringReader(jsonArrayString)); | |
JsonArray jsonArray = jsonReader.readArray(); | |
return jsonArray; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment