diff --git a/plugins/json-adapter/src/main/java/org/polypheny/db/adapter/json/JsonToPolyConverter.java b/plugins/json-adapter/src/main/java/org/polypheny/db/adapter/json/JsonToPolyConverter.java index ea35d66abe..11e10b7fb4 100644 --- a/plugins/json-adapter/src/main/java/org/polypheny/db/adapter/json/JsonToPolyConverter.java +++ b/plugins/json-adapter/src/main/java/org/polypheny/db/adapter/json/JsonToPolyConverter.java @@ -28,63 +28,65 @@ import org.polypheny.db.type.entity.PolyValue; import org.polypheny.db.type.entity.category.PolyNumber; import org.polypheny.db.type.entity.document.PolyDocument; -import org.polypheny.db.type.entity.numerical.PolyInteger; +import org.polypheny.db.type.entity.numerical.PolyDouble; +import org.polypheny.db.type.entity.numerical.PolyLong; +import org.polypheny.db.type.entity.relational.PolyMap; public class JsonToPolyConverter { - public PolyDocument nodeToDocument( JsonNode node ) throws IOException { + public PolyDocument nodeToPolyDocument( JsonNode node ) { return new PolyDocument( nodeToPolyMap( node ) ); } - public HashMap nodeToPolyMap( JsonNode node ) { - HashMap polyMap = new HashMap<>(); + public PolyMap nodeToPolyMap( JsonNode node ) { + HashMap map = new HashMap<>(); node.fields().forEachRemaining( entry -> { PolyString key = new PolyString( entry.getKey() ); PolyValue value = nodeToPolyValue( entry.getValue() ); - polyMap.put( key, value ); + map.put( key, value ); } ); - return polyMap; + return PolyMap.of( map ); } public PolyValue nodeToPolyValue( JsonNode node ) { switch ( node.getNodeType() ) { - case NULL -> { - } - case ARRAY -> { - } - case OBJECT -> { - } - case NUMBER -> { - } - case STRING -> { - } - case BOOLEAN -> { - } + case NULL -> {return nodeToPolyNull();} + case ARRAY -> {return nodeToPolyList( node );} + case OBJECT -> {return nodeToPolyMap( node );} + case NUMBER -> {return nodeToPolyNumber( node );} + case STRING -> {return nodeToPolyString( node );} + case BOOLEAN -> {return nodeToPolyBoolean( node );} } return new PolyNull(); } + public PolyNull nodeToPolyNull() { return new PolyNull(); } - public PolyString nodeToPolyString(JsonNode node) { - return new PolyString( node.textValue() ); + + public PolyString nodeToPolyString( JsonNode node ) { + return new PolyString( node.asText() ); } - public PolyBoolean nodeToPolyBoolean(JsonNode node) { - return new PolyBoolean( node.booleanValue() ); + + public PolyBoolean nodeToPolyBoolean( JsonNode node ) { + return new PolyBoolean( node.asBoolean() ); } - public PolyNumber nodeToPolyNumber(JsonNode node) { - Number number = node.numberValue(); - return new PolyInteger( 69 ); + + public PolyNumber nodeToPolyNumber( JsonNode node ) { + if ( node.isIntegralNumber() ) { + return new PolyLong( node.asLong() ); + } + return new PolyDouble( node.asDouble() ); } - public PolyValue arrayNodeToPolyList( JsonNode node ) { + public PolyValue nodeToPolyList( JsonNode node ) { return StreamSupport.stream( node.spliterator(), false ) .map( this::nodeToPolyValue ) .collect( Collectors.toCollection( PolyList::new ) ); diff --git a/plugins/json-adapter/src/test/java/org/polypheny/db/test/JsonToPolyConverterTest.java b/plugins/json-adapter/src/test/java/org/polypheny/db/test/JsonToPolyConverterTest.java index b7de245604..79c3855ddb 100644 --- a/plugins/json-adapter/src/test/java/org/polypheny/db/test/JsonToPolyConverterTest.java +++ b/plugins/json-adapter/src/test/java/org/polypheny/db/test/JsonToPolyConverterTest.java @@ -22,11 +22,17 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.polypheny.db.TestHelper; import org.polypheny.db.adapter.json.JsonToPolyConverter; +import org.polypheny.db.type.entity.PolyList; +import org.polypheny.db.type.entity.PolyNull; +import org.polypheny.db.type.entity.PolyString; import org.polypheny.db.type.entity.PolyValue; +import org.polypheny.db.type.entity.document.PolyDocument; +import org.polypheny.db.type.entity.relational.PolyMap; public class JsonToPolyConverterTest { @@ -36,7 +42,7 @@ public class JsonToPolyConverterTest { @BeforeAll public static void setup() { - //TestHelper testHelper = TestHelper.getInstance(); + TestHelper testHelper = TestHelper.getInstance(); mapper = new ObjectMapper(); builder = new JsonToPolyConverter(); @@ -47,28 +53,127 @@ private JsonNode getNodesFromJson( String json ) throws JsonProcessingException return mapper.readTree( json ); } + @Test public void testString() throws JsonProcessingException { - JsonNode node = getNodesFromJson( "\"name\": \"Maxine\"" ); + JsonNode node = getNodesFromJson( "{\"name\": \"Maxine\"}" ); PolyValue value = builder.nodeToPolyValue( node ); - assertTrue(value.isString()); - assertEquals("Maxine", value.asString().value); + assertTrue( value.isMap() ); + PolyMap map = value.asMap(); + assertEquals( "Maxine", map.get( new PolyString( "name" ) ).toString() ); } + @Test - public void testInteger() throws JsonProcessingException { - JsonNode node = getNodesFromJson( "\"integer\": 492943" ); - PolyValue value = builder.nodeToPolyValue( node ); - assertTrue(value.isInteger()); - assertEquals(492943, value.asInteger().value); + public void testLong() throws JsonProcessingException { + JsonNode node = getNodesFromJson("{\"integer\": 492943}"); + PolyValue value = builder.nodeToPolyValue(node); + assertTrue(value.isMap()); + PolyMap map = value.asMap(); + assertEquals(492943, map.get (new PolyString("integer")).asLong().value); } + + @Test public void testDouble() throws JsonProcessingException { - JsonNode node = getNodesFromJson( "\"double\": -650825.13" ); + JsonNode node = getNodesFromJson( "{\"double\": -650825.13}" ); + PolyValue value = builder.nodeToPolyValue( node ); + assertTrue( value.isMap() ); + PolyMap map = value.asMap(); + assertEquals( -650825.13, map.get(new PolyString( "double" )).asDouble().value ); + } + + + @Test + public void testBooleanTrue() throws JsonProcessingException { + JsonNode node = getNodesFromJson( "{\"boolean\": true}" ); + PolyValue value = builder.nodeToPolyValue( node ); + assertTrue( value.isMap() ); + PolyMap map = value.asMap(); + assertEquals( true, map.get(new PolyString( "boolean" )).asBoolean().value ); + } + + @Test + public void testBooleanFalse() throws JsonProcessingException { + JsonNode node = getNodesFromJson( "{\"boolean\": false}" ); PolyValue value = builder.nodeToPolyValue( node ); - assertTrue(value.isDouble()); - assertEquals(-650825.13, value.asDouble().value); + assertTrue( value.isMap() ); + PolyMap map = value.asMap(); + assertEquals( false, map.get(new PolyString( "boolean" )).asBoolean().value ); } + @Test + public void testArray() throws JsonProcessingException { + JsonNode node = getNodesFromJson( "{\"integers\": [0, 1, 2, 3]}" ); + PolyValue value = builder.nodeToPolyValue( node ); + assertTrue( value.isMap() ); + PolyMap map = value.asMap(); + assertTrue(map.get(new PolyString( "integers" )).isList()); + PolyList list = map.get(new PolyString( "integers" )).asList(); + assertEquals( 0, list.get(0).asLong().value ); + assertEquals( 1, list.get(1).asLong().value ); + assertEquals( 2, list.get(2).asLong().value ); + assertEquals( 3, list.get(3).asLong().value ); + } + + @Test + public void testNull() throws JsonProcessingException { + JsonNode node = getNodesFromJson( "{\"null\": null}" ); + PolyValue value = builder.nodeToPolyValue( node ); + assertTrue( value.isMap() ); + PolyMap map = value.asMap(); + assertTrue(map.get(new PolyString( "null" )).isNull()); + } + + @Test + public void testDocument() throws IOException { + String json = "{" + + "\"string\": \"Hello, JSON!\"," + + "\"number\": 12345.678," + + "\"boolean\": true," + + "\"null\": null," + + "\"object\": {" + + " \"nestedString\": \"Inside JSON\"," + + " \"nestedNumber\": 9876" + + "}," + + "\"array\": [" + + " \"item1\"," + + " 234," + + " false," + + " null" + + "]" + + "}"; + + JsonNode node = getNodesFromJson( json ); + PolyValue value = builder.nodeToPolyDocument( node ); + assertTrue( value.isDocument()); + PolyDocument doc = value.asDocument(); + assertEquals( "Hello, JSON!", doc.get( new PolyString( "string" ) ).asString().getValue() ); + assertEquals( 12345.678, doc.get( new PolyString( "number" ) ).asDouble().getValue() ); + assertEquals( true, doc.get( new PolyString( "boolean" ) ).asBoolean().getValue() ); + assertEquals( new PolyNull(), doc.get( new PolyString( "null" ) ).asNull() ); + + // check nested object + assertTrue( doc.get( new PolyString( "object" ) ).isMap() ); + PolyMap nestedObject = doc.get( new PolyString( "object" ) ).asMap(); + assertEquals( "Inside JSON", nestedObject.get(new PolyString( "nestedString" )).asString().getValue() ); + assertEquals( 9876, nestedObject.get(new PolyString( "nestedNumber" )).asLong().getValue() ); + + // check array + assertTrue( doc.get( new PolyString( "array" ) ).isList() ); + PolyList list = doc.get( new PolyString( "array" ) ).asList(); + assertTrue(list.get(0).isString()); + assertTrue(list.get(1).isLong()); + assertTrue(list.get(2).isBoolean()); + assertTrue(list.get(3).isNull()); + + assertEquals( "item1", list.get(0).asString().getValue() ); + assertEquals( 234, list.get(1).asLong().getValue() ); + assertEquals( false, list.get(2).asBoolean().getValue() ); + assertEquals( new PolyNull(), list.get(3).asNull() ); + } + + + }