Skip to content

Latest commit

History

History
433 lines (303 loc) · 11.6 KB

File metadata and controls

433 lines (303 loc) · 11.6 KB

Examples

Imports used in the examples:

importjava.util.HashMap;
importjava.util.Map;
importjava.util.Properties;

This document's intention is to explain to new-comers the basics of this project

Part 1: Creating a JSON document

Using JSONArray

privatestaticvoidJSONExampleArray1() {
//We create a JSONObject from a String containing an array using JSONArray//Firstly, we declare an Array in a StringStringarrayStr = "["+"true,"+"false,"+ "\"true\","+ "\"false\","+"\"hello\","+"23.45e-4,"+
"\"23.45\","+"42,"+"\"43\","+"["+"\"world\""+"],"+
"{"+
"\"key1\":\"value1\","+
"\"key2\":\"value2\","+
"\"key3\":\"value3\","+
"\"key4\":\"value4\""+
"},"+
"0,"+"\"-1\""+
"]";
//Then, we initializate the JSONArray thanks to its constructorJSONArrayarray = newJSONArray(arrayStr);
System.out.println("Values array: "+ array);
//We convert that array into a JSONObject, but first, we need the labels, so we need another JSONArray with the labels.//Here we will use an auxiliary function to get one for the example.JSONArraylist = listNumberArray(array.length());
System.out.println("Label Array: "+ list.toString());
//Now, we construct the JSONObject using both the value array and the label array.JSONObjectobject = array.toJSONObject(list);
System.out.println("Final JSONOBject: " + object);
}
//This method creates an JSONArray of labels in which those are generated by their positionsprivatestaticJSONArraylistNumberArray(intmax){
JSONArrayres = newJSONArray();
for (inti=0; i<max;i++) {
//The value of the labels must be an String in order to make it workres.put(String.valueOf(i));
}
returnres;
}
privatestaticvoidJSONExampleArray2() {
//We can also create an Array without a String by creating an empty array and adding elements to itJSONArrayarray = newJSONArray();
//Adding elements with .put()array.put("value");
array.put(5);
array.put(-23.45e67);
array.put(true);
//We convert it to JSONObject providing a label arrray like last timeJSONArraylist = listNumberArray(array.length());
JSONObjectobject = array.toJSONObject(list);
System.out.println("Final JSONOBject: " + object);
}

Using JSONStringer

privatestaticvoidJSONExampleStringer() {
//We initializate the JSONStringerJSONStringerjsonStringer = newJSONStringer();
//Now we start the process of adding elements with .object()jsonStringer.object();
//We can now add elements as keys and values with .values () and .key()jsonStringer.key("trueValue").value(true);
jsonStringer.key("falseValue").value(false);
jsonStringer.key("nullValue").value(null);
jsonStringer.key("stringValue").value("hello world!");
jsonStringer.key("complexStringValue").value("h\be\tllo w\u1234orld!");
jsonStringer.key("intValue").value(42);
jsonStringer.key("doubleValue").value(-23.45e67);
//We end this prcedure with .ednObjectjsonStringer.endObject();
//Once we have a JSONStringer, we convert it to JSONObject generating a String and using JSONObject's contructor.Stringstr = jsonStringer.toString();
JSONObjectjsonObject = newJSONObject(str);
System.out.println("Final JSONOBject: " + jsonObject);
}

Using JSONObject

privatestaticvoidJSONExampleObject1() {
//We can create a JSONObject from a String with the class builderStringstring = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
JSONObjectexample = newJSONObject(string);
System.out.println("Final JSONObject: " + example);
}
privatestaticvoidJSONExampleObject2() {
//We can also create a JSONObject directly without messing around with any of the other functions.JSONObjectexample = newJSONObject();
//Now we add the keys and values in a similar way as the Stringer methodexample.put("key", "value");
//As you can see, the first entry is the key and the second would be its associeted value.example.put("key2", 5);
example.put("key3", -23.45e67);
example.put("trueValue", true);
//We can't add null values thougth//example.put("nullValue", null); //This is not possibleSystem.out.println("Final JSONOBject: " + example);
}
privatestaticvoidJSONExampleObject3() {
//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 wantMap<String,Double> map = newHashMap<String, Double>();
map.put("key1", 1.0);
map.put("key2", -23.45e67);
//We create the JSONObject with the map with its class builderJSONObjectexample = newJSONObject(map);
System.out.println("Final JSONOBject: " + example);
}

Using JSONWriter

privatestaticvoidJSONExamplWriter() {
//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 StringBuilderStringBuilderwrite = newStringBuilder();
JSONWriterjsonWriter = newJSONWriter(write);
//We behave now the same way as StringerjsonWriter.object();
jsonWriter.key("trueValue").value(true);
jsonWriter.key("falseValue").value(false);
jsonWriter.key("nullValue").value(null);
jsonWriter.key("stringValue").value("hello world!");
jsonWriter.key("complexStringValue").value("h\be\tllo w\u1234orld!");
jsonWriter.key("intValue").value(42);
jsonWriter.key("doubleValue").value(-23.45e67);
jsonWriter.endObject();
//The resoult should be in the "write" objectSystem.out.println("JSON: " + write.toString());
//The difference is that we don't get a JSONObject in this one.
}
privatestaticvoidJSONExampleTokener() {
//A partir de una String podemos crear un JSONTokener, que lo podemos usar alternativamente para JSONArray,JSONObjectStringstring = "this is not a valid JSON string";
JSONTokenertoken = newJSONTokener(string);
//Now you can use the token in JSONObject and Array the same way as a StringJSONObjectobject = newJSONObject(token);
JSONArrayarray = newJSONArray(token);
}

Part 2: Conversion methods

We don't need to have a JSON document to work. This project also admits conversions from other type of files.

Secondly, we can also convert from JSON to those type of files.

Extra: Conversion to JSONArray

privatestaticvoidJSONObjectToArray() {
//We start with a JSONObjectStringstring = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
JSONObjectexample = newJSONObject(string);
//We need a list of key strings like the reverse operationJSONArraykeyStrings = listNumberArray(example.length());
//Then we convert to the Array using both elelementsJSONArrayarray = example.toJSONArray(keyStrings);
System.out.println("Final JSONArray: " + array);
}

XML Conversions

privatestaticvoidXMLToExampleConversion() {
//We start with a JSONObjectStringstring = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
JSONObjectexample = newJSONObject(string);
//We obtain a String with XML format with toString()Stringoutput = XML.toString(example);
System.out.println("Final XML: " + output);
}
privatestaticvoidXMLFromExampleConversion() {
//We start with a string with the XML formatStringstring = "<0>value</0><1>5</1><2>-2.345E+68</2><3>true</3>";
//We obtain a JSONObject with toJSONOBject()JSONObjectoutput = XML.toJSONObject(string);
System.out.println("Final JSONObject: " + output);
}

Cookie Conversions

privatestaticvoidCookieToExampleConversion() {
//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 Cokkie format doesn't support booleansStringstring = "{\"name\":\"Cookie-Name\",\"value\":\"name\",\"1\":5,\"2\":-2.345E68,\"3\":'true'}";
JSONObjectexample = newJSONObject(string);
//We obtain a String with Cookie format with toString()Stringoutput = Cookie.toString(example);
System.out.println("Final Cookie: " + output);
}
privatestaticvoidCookieFromExampleConversion() {
//We start with a string with the Cookie formatStringstring = "Cookie-Name=name;1=5;2=-2.345E%2b68;3=true";
//We obtain a JSONObject with toJSONOBject()JSONObjectoutput = Cookie.toJSONObject(string);
System.out.println("Final JSONObject: " + output);
}

HTTP Conversions

privatestaticvoidHTTPToExampleConversion() {
//We start with a JSONObject//The JSONObject must have the minimun header for a HTTP request or headerStringstring = "{\"Method\":\"POST\",\"Request-URI\":'/',\"HTTP-Version\":'HTTP/1.1',\"Value1\":true,\"Value2\":2,\"Value3\":-2.345E68}";
JSONObjectexample = newJSONObject(string);
//We obtain a String with HTTP format with toString()Stringoutput = HTTP.toString(example);
System.out.println("Final HTTP: " + output);
}
privatestaticvoidHTTPFromExampleConversion() {
//We start with a string with the HTTP formatStringstring = "Final HTTP: POST '/' HTTP/1.1 Value3: -2.345E+68 Value1: true Value2: 2";
//We obtain a JSONObject with toJSONOBject()JSONObjectoutput = HTTP.toJSONObject(string);
System.out.println("Final JSONObject: " + output);
}

CDL Conversions

privatestaticvoidCDLToExampleConversion() {
//We start with some JSONObjects with the same values in the keys but different values in the "values"Stringstring = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
JSONObjectexample = newJSONObject(string);
Stringstring2 = "{\"0\":\"value2\",\"1\":6,\"2\":-8.345E68,\"3\":false}";
JSONObjectexample2 = newJSONObject(string2);
//We need now a JSONArray with those JSONObjectsJSONArrayarray = newJSONArray();
array.put(example);
array.put(example2);
//We obtain a String with XML format with toString()Stringoutput = CDL.toString(array);
System.out.println("Final CDL: \r\n" + output);
}
privatestaticvoidCDLFromExampleConversion() {
//We start wtih a String with the CDL formatStringstring = "0,1,2,3\n"
+ "value,5,-2.345E+68,true\n"
+ "value2,6,-8.345E+68,false";
//We obtain a JSONArray with toJSONOBject()JSONArrayoutput = CDL.toJSONArray(string);
System.out.println("Final JSONArray: " + output);
}

Properties Conversions

privatestaticPropertiesPropertyToExampleConversion() {
//We start with a JSONObjectStringstring = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}";
JSONObjectexample = newJSONObject(string);
//We obtain a String with Properties format with toString()Propertiesoutput = Property.toProperties(example);
System.out.println("Final Properties: " + output);
returnoutput;
}
privatestaticvoidPropertyFromExampleConversion() {
//We start with a Properties objectPropertiesinput = PropertyToExampleConversion();
//We obtain a JSONObject with toJSONOBject()JSONObjectoutput = Property.toJSONObject(input);
System.out.println("Final JSONObject: " + output);
}

List of all examples methods

publicstaticvoidmain(String[] args) {
//JSONObjectToArray();//JSONExampleArray1();//JSONExampleArray2();//JSONExampleStringer();//JSONExampleObject1();//JSONExampleObject2();//JSONExampleObject3();//JSONExamplWriter();//XMLToExampleConversion();//XMLFromExampleConversion();//CookieToExampleConversion();//CookieFromExampleConversion(); //HTTPToExampleConversion();//HTTPFromExampleConversion();//CDLToExampleConversion();//CDLFromExampleConversion();//PropertyToExampleConversion();//PropertyFromExampleConversion();
}