Merge branch 'master' of https://github.com/stleary/JSON-java into Pretty-Print-XML-Functionality

This commit is contained in:
Dean 2022-10-10 11:12:49 +01:00
commit bf9219386a
No known key found for this signature in database
GPG Key ID: B0FA03846A25C037

View File

@ -1,7 +1,7 @@
<h1>Examples</h1> <h1>Examples</h1>
<p>Imports used in the examples: </p> <p>Imports used in the examples: </p>
``` ```java
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -14,8 +14,8 @@ import java.util.Properties;
<h3>Using JSONArray</h3> <h3>Using JSONArray</h3>
``` ```java
private static void JSONExampleArray1() { private static void JSONExampleArray1() {
//We create a JSONObject from a String containing an array using JSONArray //We create a JSONObject from a String containing an array using JSONArray
//Firstly, we declare an Array in a String //Firstly, we declare an Array in a String
@ -44,21 +44,21 @@ import java.util.Properties;
//Now, we construct the JSONObject using both the value array and the label array. //Now, we construct the JSONObject using both the value array and the label array.
JSONObject object = array.toJSONObject(list); JSONObject object = array.toJSONObject(list);
System.out.println("Final JSONOBject: " + object); System.out.println("Final JSONOBject: " + object);
} }
//This method creates an JSONArray of labels in which those are generated by their positions //This method creates an JSONArray of labels in which those are generated by their positions
private static JSONArray listNumberArray(int max){ private static JSONArray listNumberArray(int max){
JSONArray res = new JSONArray(); JSONArray res = new JSONArray();
for (int i=0; i<max;i++) { for (int i=0; i<max;i++) {
//The value of the labels must be an String in order to make it work //The value of the labels must be an String in order to make it work
res.put(String.valueOf(i)); res.put(String.valueOf(i));
} }
return res; return res;
} }
``` ```
``` ```java
private static void JSONExampleArray2() { private static void JSONExampleArray2() {
//We can also create an Array without a String by creating an empty array and adding elements to it //We can also create an Array without a String by creating an empty array and adding elements to it
@ -76,13 +76,13 @@ import java.util.Properties;
JSONArray list = listNumberArray(array.length()); JSONArray list = listNumberArray(array.length());
JSONObject object = array.toJSONObject(list); JSONObject object = array.toJSONObject(list);
System.out.println("Final JSONOBject: " + object); System.out.println("Final JSONOBject: " + object);
} }
``` ```
<h3>Using JSONStringer</h3> <h3>Using JSONStringer</h3>
``` ```java
private static void JSONExampleStringer() { private static void JSONExampleStringer() {
//We initializate the JSONStringer //We initializate the JSONStringer
@ -112,12 +112,12 @@ import java.util.Properties;
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
System.out.println("Final JSONOBject: " + jsonObject); System.out.println("Final JSONOBject: " + jsonObject);
} }
``` ```
<h3>Using JSONObject</h3> <h3>Using JSONObject</h3>
``` ```java
private static void JSONExampleObject1() { private static void JSONExampleObject1() {
//We can create a JSONObject from a String with the class builder //We can create a JSONObject from a String with the class builder
@ -125,10 +125,10 @@ import java.util.Properties;
JSONObject example = new JSONObject(string); JSONObject example = new JSONObject(string);
System.out.println("Final JSONObject: " + example); System.out.println("Final JSONObject: " + example);
} }
``` ```
``` ```java
private static void JSONExampleObject2() { private static void JSONExampleObject2() {
//We can also create a JSONObject directly without messing around with any of the other functions. //We can also create a JSONObject directly without messing around with any of the other functions.
@ -149,10 +149,10 @@ import java.util.Properties;
//example.put("nullValue", null); //This is not possible //example.put("nullValue", null); //This is not possible
System.out.println("Final JSONOBject: " + example); System.out.println("Final JSONOBject: " + example);
} }
``` ```
``` ```java
private static void JSONExampleObject3() { private static void JSONExampleObject3() {
//We can also create a JSONObject with a Java Map //We can also create a JSONObject with a Java Map
//YoU will need a Map whose keys are Strings. The values can be whatever you want //YoU will need a Map whose keys are Strings. The values can be whatever you want
@ -166,12 +166,12 @@ import java.util.Properties;
JSONObject example = new JSONObject(map); JSONObject example = new JSONObject(map);
System.out.println("Final JSONOBject: " + example); System.out.println("Final JSONOBject: " + example);
} }
``` ```
<h3>Using JSONWriter</h3> <h3>Using JSONWriter</h3>
``` ```java
private static void JSONExamplWriter() { private static void JSONExamplWriter() {
//This method works in a very similar way to Object and Stringer in the construction of the JSON. //This method works in a very similar way to Object and Stringer in the construction of the JSON.
//The difference is that it needs a Java object called "Appendable" like StringBuilder //The difference is that it needs a Java object called "Appendable" like StringBuilder
@ -200,10 +200,10 @@ import java.util.Properties;
//The difference is that we don't get a JSONObject in this one. //The difference is that we don't get a JSONObject in this one.
} }
``` ```
``` ```java
private static void JSONExampleTokener() { private static void JSONExampleTokener() {
//A partir de una String podemos crear un JSONTokener, que lo podemos usar alternativamente para JSONArray,JSONObject //A partir de una String podemos crear un JSONTokener, que lo podemos usar alternativamente para JSONArray,JSONObject
@ -215,7 +215,7 @@ import java.util.Properties;
JSONObject object = new JSONObject(token); JSONObject object = new JSONObject(token);
JSONArray array = new JSONArray(token); JSONArray array = new JSONArray(token);
} }
``` ```
<h2>Part 2: Conversion methods</h2> <h2>Part 2: Conversion methods</h2>
<p>We don't need to have a JSON document to work. This project also admits conversions from other type of files.</p> <p>We don't need to have a JSON document to work. This project also admits conversions from other type of files.</p>
@ -223,8 +223,8 @@ import java.util.Properties;
<h3>Extra: Conversion to JSONArray</h3> <h3>Extra: Conversion to JSONArray</h3>
``` ```java
private static void JSONObjectToArray() { private static void JSONObjectToArray() {
//We start with a JSONObject //We start with a JSONObject
String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}"; String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
@ -240,12 +240,12 @@ import java.util.Properties;
JSONArray array = example.toJSONArray(keyStrings); JSONArray array = example.toJSONArray(keyStrings);
System.out.println("Final JSONArray: " + array); System.out.println("Final JSONArray: " + array);
} }
``` ```
<h3>XML Conversions</h3> <h3>XML Conversions</h3>
``` ```java
private static void XMLToExampleConversion() { private static void XMLToExampleConversion() {
//We start with a JSONObject //We start with a JSONObject
@ -256,10 +256,10 @@ import java.util.Properties;
String output = XML.toString(example); String output = XML.toString(example);
System.out.println("Final XML: " + output); System.out.println("Final XML: " + output);
} }
``` ```
``` ```java
private static void XMLFromExampleConversion() { private static void XMLFromExampleConversion() {
//We start with a string with the XML format //We start with a string with the XML format
@ -270,12 +270,12 @@ import java.util.Properties;
JSONObject output = XML.toJSONObject(string); JSONObject output = XML.toJSONObject(string);
System.out.println("Final JSONObject: " + output); System.out.println("Final JSONObject: " + output);
} }
``` ```
<h3>Cookie Conversions</h3> <h3>Cookie Conversions</h3>
``` ```java
private static void CookieToExampleConversion() { private static void CookieToExampleConversion() {
//We start with a JSONObject //We start with a JSONObject
//The JSONOBject needs to entries that gives the cookie a name and gives the field "name" a name too. //The JSONOBject needs to entries that gives the cookie a name and gives the field "name" a name too.
@ -288,10 +288,10 @@ import java.util.Properties;
String output = Cookie.toString(example); String output = Cookie.toString(example);
System.out.println("Final Cookie: " + output); System.out.println("Final Cookie: " + output);
} }
``` ```
``` ```java
private static void CookieFromExampleConversion() { private static void CookieFromExampleConversion() {
//We start with a string with the Cookie format //We start with a string with the Cookie format
@ -301,13 +301,13 @@ import java.util.Properties;
JSONObject output = Cookie.toJSONObject(string); JSONObject output = Cookie.toJSONObject(string);
System.out.println("Final JSONObject: " + output); System.out.println("Final JSONObject: " + output);
} }
``` ```
<h3>HTTP Conversions</h3> <h3>HTTP Conversions</h3>
``` ```java
private static void HTTPToExampleConversion() { private static void HTTPToExampleConversion() {
//We start with a JSONObject //We start with a JSONObject
//The JSONObject must have the minimun header for a HTTP request or header //The JSONObject must have the minimun header for a HTTP request or header
@ -320,10 +320,10 @@ import java.util.Properties;
String output = HTTP.toString(example); String output = HTTP.toString(example);
System.out.println("Final HTTP: " + output); System.out.println("Final HTTP: " + output);
} }
``` ```
``` ```java
private static void HTTPFromExampleConversion() { private static void HTTPFromExampleConversion() {
//We start with a string with the HTTP format //We start with a string with the HTTP format
@ -333,11 +333,11 @@ import java.util.Properties;
JSONObject output = HTTP.toJSONObject(string); JSONObject output = HTTP.toJSONObject(string);
System.out.println("Final JSONObject: " + output); System.out.println("Final JSONObject: " + output);
} }
``` ```
<h3>CDL Conversions</h3> <h3>CDL Conversions</h3>
``` ```java
private static void CDLToExampleConversion() { private static void CDLToExampleConversion() {
//We start with some JSONObjects with the same values in the keys but different values in the "values" //We start with some JSONObjects with the same values in the keys but different values in the "values"
@ -358,9 +358,9 @@ private static void CDLToExampleConversion() {
String output = CDL.toString(array); String output = CDL.toString(array);
System.out.println("Final CDL: \r\n" + output); System.out.println("Final CDL: \r\n" + output);
} }
```
``` ```
```java
private static void CDLFromExampleConversion() { private static void CDLFromExampleConversion() {
//We start wtih a String with the CDL format //We start wtih a String with the CDL format
@ -377,8 +377,8 @@ private static void CDLFromExampleConversion() {
``` ```
<h3>Properties Conversions</h3> <h3>Properties Conversions</h3>
``` ```java
private static Properties PropertyToExampleConversion() { private static Properties PropertyToExampleConversion() {
//We start with a JSONObject //We start with a JSONObject
@ -391,10 +391,10 @@ private static void CDLFromExampleConversion() {
System.out.println("Final Properties: " + output); System.out.println("Final Properties: " + output);
return output; return output;
} }
``` ```
``` ```java
private static void PropertyFromExampleConversion() { private static void PropertyFromExampleConversion() {
//We start with a Properties object //We start with a Properties object
@ -404,12 +404,12 @@ private static void CDLFromExampleConversion() {
JSONObject output = Property.toJSONObject(input); JSONObject output = Property.toJSONObject(input);
System.out.println("Final JSONObject: " + output); System.out.println("Final JSONObject: " + output);
} }
``` ```
<h2>List of all examples methods</h2> <h2>List of all examples methods</h2>
``` ```java
public static void main(String[] args) { public static void main(String[] args) {
//JSONObjectToArray(); //JSONObjectToArray();
//JSONExampleArray1(); //JSONExampleArray1();
//JSONExampleArray2(); //JSONExampleArray2();
@ -428,6 +428,6 @@ private static void CDLFromExampleConversion() {
//CDLFromExampleConversion(); //CDLFromExampleConversion();
//PropertyToExampleConversion(); //PropertyToExampleConversion();
//PropertyFromExampleConversion(); //PropertyFromExampleConversion();
} }
``` ```