Compare commits

..

5 Commits

Author SHA1 Message Date
Sean Leary d51250f6b0 Merge pull request #692 from InACommandBlock/patch-1
Example.md syntax highlight and indentation
2022-10-09 22:28:36 -05:00
Sean Leary 444335d12a Merge pull request #691 from hendrixjoseph/issue-657-unit-tests-should-check-various-number-formats
create unit tests for various number formats
2022-10-07 11:18:41 -05:00
TheCommandBlock 12411b7981 Update Examples.md
Co-authored-by: JAYSE <104235500+JayseMayne@users.noreply.github.com>
2022-10-06 03:18:03 +02:00
TheCommandBlock 61801c623e Minor Adjustments Example.md
Added syntax highlighting, standardised indentation
2022-10-06 00:48:34 +02:00
hendrixjoseph 1915aab7c4 create unit tests for various number formats 2022-10-04 14:32:41 -04:00
2 changed files with 412 additions and 286 deletions
+62 -62
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>
<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();
}
}
```
@@ -0,0 +1,126 @@
package org.json.junit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(value = Parameterized.class)
public class JSONObjectNumberTest {
private final String objectString;
private Integer value = 50;
@Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"{value:50}", 1},
{"{value:50.0}", 1},
{"{value:5e1}", 1},
{"{value:5E1}", 1},
{"{value:5e1}", 1},
{"{value:'50'}", 1},
{"{value:-50}", -1},
{"{value:-50.0}", -1},
{"{value:-5e1}", -1},
{"{value:-5E1}", -1},
{"{value:-5e1}", -1},
{"{value:'-50'}", -1}
// JSON does not support octal or hex numbers;
// see https://stackoverflow.com/a/52671839/6323312
// "{value:062}", // octal 50
// "{value:0x32}" // hex 50
});
}
public JSONObjectNumberTest(String objectString, int resultIsNegative) {
this.objectString = objectString;
this.value *= resultIsNegative;
}
private JSONObject object;
@Before
public void setJsonObject() {
object = new JSONObject(objectString);
}
@Test
public void testGetNumber() {
assertEquals(value.intValue(), object.getNumber("value").intValue());
}
@Test
public void testGetBigDecimal() {
assertTrue(BigDecimal.valueOf(value).compareTo(object.getBigDecimal("value")) == 0);
}
@Test
public void testGetBigInteger() {
assertEquals(BigInteger.valueOf(value), object.getBigInteger("value"));
}
@Test
public void testGetFloat() {
assertEquals(value.floatValue(), object.getFloat("value"), 0.0f);
}
@Test
public void testGetDouble() {
assertEquals(value.doubleValue(), object.getDouble("value"), 0.0d);
}
@Test
public void testGetInt() {
assertEquals(value.intValue(), object.getInt("value"));
}
@Test
public void testGetLong() {
assertEquals(value.longValue(), object.getLong("value"));
}
@Test
public void testOptNumber() {
assertEquals(value.intValue(), object.optNumber("value").intValue());
}
@Test
public void testOptBigDecimal() {
assertTrue(BigDecimal.valueOf(value).compareTo(object.optBigDecimal("value", null)) == 0);
}
@Test
public void testOptBigInteger() {
assertEquals(BigInteger.valueOf(value), object.optBigInteger("value", null));
}
@Test
public void testOptFloat() {
assertEquals(value.floatValue(), object.optFloat("value"), 0.0f);
}
@Test
public void testOptDouble() {
assertEquals(value.doubleValue(), object.optDouble("value"), 0.0d);
}
@Test
public void testOptInt() {
assertEquals(value.intValue(), object.optInt("value"));
}
@Test
public void testOptLong() {
assertEquals(value.longValue(), object.optLong("value"));
}
}