Minor Adjustments Example.md

Added syntax highlighting, standardised indentation
This commit is contained in:
TheCommandBlock 2022-10-06 00:48:34 +02:00 committed by GitHub
parent 8439039da7
commit 61801c623e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
<h1>Examples</h1>
<p>Imports used in the examples: </p>
```
```java
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@ -14,8 +14,8 @@ import java.util.Properties;
<h3>Using JSONArray</h3>
```
private static void JSONExampleArray1() {
```java
private static void JSONExampleArray1() {
//We create a JSONObject from a String containing an array using JSONArray
//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.
JSONObject object = array.toJSONObject(list);
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();
for (int i=0; i<max;i++) {
//The value of the labels must be an String in order to make it work
res.put(String.valueOf(i));
}
return res;
}
}
```
```
private static void JSONExampleArray2() {
```java
private static void JSONExampleArray2() {
//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());
JSONObject object = array.toJSONObject(list);
System.out.println("Final JSONOBject: " + object);
}
}
```
<h3>Using JSONStringer</h3>
```
private static void JSONExampleStringer() {
```java
private static void JSONExampleStringer() {
//We initializate the JSONStringer
@ -112,12 +112,12 @@ import java.util.Properties;
JSONObject jsonObject = new JSONObject(str);
System.out.println("Final JSONOBject: " + jsonObject);
}
}
```
<h3>Using JSONObject</h3>
```
private static void JSONExampleObject1() {
```java
private static void JSONExampleObject1() {
//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);
System.out.println("Final JSONObject: " + example);
}
}
```
```
private static void JSONExampleObject2() {
```java
private static void JSONExampleObject2() {
//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
System.out.println("Final JSONOBject: " + example);
}
}
```
```
private static void JSONExampleObject3() {
```java
private static void JSONExampleObject3() {
//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
@ -166,12 +166,12 @@ import java.util.Properties;
JSONObject example = new JSONObject(map);
System.out.println("Final JSONOBject: " + example);
}
}
```
<h3>Using JSONWriter</h3>
```
private static void JSONExamplWriter() {
```java
private static void JSONExamplWriter() {
//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
@ -200,10 +200,10 @@ import java.util.Properties;
//The difference is that we don't get a JSONObject in this one.
}
}
```
```
private static void JSONExampleTokener() {
```java
private static void JSONExampleTokener() {
//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);
JSONArray array = new JSONArray(token);
}
}
```
<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>
@ -223,8 +223,8 @@ import java.util.Properties;
<h3>Extra: Conversion to JSONArray</h3>
```
private static void JSONObjectToArray() {
```java
private static void JSONObjectToArray() {
//We start with a JSONObject
String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
@ -240,12 +240,12 @@ import java.util.Properties;
JSONArray array = example.toJSONArray(keyStrings);
System.out.println("Final JSONArray: " + array);
}
}
```
<h3>XML Conversions</h3>
```
private static void XMLToExampleConversion() {
```java
private static void XMLToExampleConversion() {
//We start with a JSONObject
@ -256,10 +256,10 @@ import java.util.Properties;
String output = XML.toString(example);
System.out.println("Final XML: " + output);
}
}
```
```
private static void XMLFromExampleConversion() {
```java
private static void XMLFromExampleConversion() {
//We start with a string with the XML format
@ -270,12 +270,12 @@ import java.util.Properties;
JSONObject output = XML.toJSONObject(string);
System.out.println("Final JSONObject: " + output);
}
}
```
<h3>Cookie Conversions</h3>
```
private static void CookieToExampleConversion() {
```java
private static void CookieToExampleConversion() {
//We start with a JSONObject
//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);
System.out.println("Final Cookie: " + output);
}
}
```
```
private static void CookieFromExampleConversion() {
```java
private static void CookieFromExampleConversion() {
//We start with a string with the Cookie format
@ -301,13 +301,13 @@ import java.util.Properties;
JSONObject output = Cookie.toJSONObject(string);
System.out.println("Final JSONObject: " + output);
}
}
```
<h3>HTTP Conversions</h3>
```
private static void HTTPToExampleConversion() {
```java
private static void HTTPToExampleConversion() {
//We start with a JSONObject
//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);
System.out.println("Final HTTP: " + output);
}
}
```
```
private static void HTTPFromExampleConversion() {
```java
private static void HTTPFromExampleConversion() {
//We start with a string with the HTTP format
@ -333,11 +333,11 @@ import java.util.Properties;
JSONObject output = HTTP.toJSONObject(string);
System.out.println("Final JSONObject: " + output);
}
}
```
<h3>CDL Conversions</h3>
```
```java
private static void CDLToExampleConversion() {
//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);
System.out.println("Final CDL: \r\n" + output);
}
```
}
```
```java
private static void CDLFromExampleConversion() {
//We start wtih a String with the CDL format
@ -377,8 +377,8 @@ private static void CDLFromExampleConversion() {
```
<h3>Properties Conversions</h3>
```
private static Properties PropertyToExampleConversion() {
```java
private static Properties PropertyToExampleConversion() {
//We start with a JSONObject
@ -391,10 +391,10 @@ private static void CDLFromExampleConversion() {
System.out.println("Final Properties: " + output);
return output;
}
}
```
```
private static void PropertyFromExampleConversion() {
```java
private static void PropertyFromExampleConversion() {
//We start with a Properties object
@ -404,12 +404,12 @@ private static void CDLFromExampleConversion() {
JSONObject output = Property.toJSONObject(input);
System.out.println("Final JSONObject: " + output);
}
}
```
<h2>List of all examples methods</h2>
```
public static void main(String[] args) {
```java
public static void main(String[] args) {
//JSONObjectToArray();
//JSONExampleArray1();
//JSONExampleArray2();
@ -428,6 +428,6 @@ private static void CDLFromExampleConversion() {
//CDLFromExampleConversion();
//PropertyToExampleConversion();
//PropertyFromExampleConversion();
}
}
```