From e35646c381eb0c72fb07ea67715f6574e71ddd15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Merino=20Garc=C3=ADa?= Date: Sat, 23 Jan 2021 19:13:54 +0100 Subject: [PATCH] 9.0.0 rc3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 9.0.0 rc2 (#162) * feat: 🎸 streamValues methods * ci: 🎡 9.0.0-RC2 * feat: 🎸 parse yaml to json * feat: 🎸 parse yaml to json --- docs/README.md | 5 +- pom.xml | 17 +++-- src/main/java/jsonvalues/JacksonFactory.java | 4 +- src/main/java/jsonvalues/JsArray.java | 21 ++++++ src/main/java/jsonvalues/JsObj.java | 18 +++++ src/test/java/jsonvalues/TestJsObjParser.java | 67 +++++++++++++++++++ 6 files changed, 124 insertions(+), 8 deletions(-) diff --git a/docs/README.md b/docs/README.md index d0d13d3e..236db6e7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,7 +8,8 @@ [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=imrafaelmerino_json-values&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=imrafaelmerino_json-values) [![Javadocs](https://www.javadoc.io/badge/com.github.imrafaelmerino/json-values.svg)](https://www.javadoc.io/doc/com.github.imrafaelmerino/json-values) -[![Maven](https://img.shields.io/maven-central/v/com.github.imrafaelmerino/json-values/9.0.0-RC2)](https://search.maven.org/artifact/com.github.imrafaelmerino/json-values/9.0.0-RC2/jar) + +[![Maven](https://img.shields.io/maven-central/v/com.github.imrafaelmerino/json-values/9.0.0-RC3)](https://search.maven.org/artifact/com.github.imrafaelmerino/json-values/9.0.0-RC3/jar) [![](https://jitpack.io/v/imrafaelmerino/json-values.svg)](https://jitpack.io/#imrafaelmerino/json-values) [![Gitter](https://badges.gitter.im/json-values/community.svg)](https://gitter.im/json-values/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) @@ -170,7 +171,7 @@ Add the following dependency to your building tool: com.github.imrafaelmerino json-values - 9.0.0-RC2 + 9.0.0-RC3 ``` diff --git a/pom.xml b/pom.xml index 51692796..ecde914e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,9 +6,8 @@ com.github.imrafaelmerino json-values jar - 9.0.0-RC2 - - json-values + 9.0.0-RC3 + json-values json-values is a functional Java library to work with immutable jsons using persistent data structures. https://github.com/imrafaelmerino/json-values @@ -43,13 +42,14 @@ 2.22.0 0.1 1.9.7 - 0.10.3 + 0.10.3 + 2.9.5 io.vavr vavr - ${vavr.version} + ${VAVR.VERSION} @@ -70,12 +70,19 @@ ${JACKSON-VERSION} + + org.junit.jupiter junit-jupiter-api 5.3.2 test + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + ${JACKSON-DATAFORMAT-YAML.VERSION} + diff --git a/src/main/java/jsonvalues/JacksonFactory.java b/src/main/java/jsonvalues/JacksonFactory.java index b7d23f95..492974ff 100644 --- a/src/main/java/jsonvalues/JacksonFactory.java +++ b/src/main/java/jsonvalues/JacksonFactory.java @@ -1,11 +1,13 @@ package jsonvalues; import com.fasterxml.jackson.core.JsonFactory; - +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; class JacksonFactory { static final JsonFactory INSTANCE = new JsonFactory(); + static final YAMLFactory YAML_FACTORY = new YAMLFactory(); + private JacksonFactory() { } } diff --git a/src/main/java/jsonvalues/JsArray.java b/src/main/java/jsonvalues/JsArray.java index 1c0a4a05..5756a696 100644 --- a/src/main/java/jsonvalues/JsArray.java +++ b/src/main/java/jsonvalues/JsArray.java @@ -364,6 +364,27 @@ public static JsArray ofIterable(Iterable iterable) { ); } + /** + Tries to parse the YAML string into an immutable json array. + + @param str the YAML to be parsed + @return a JsArray + @throws MalformedJson if the string doesnt represent a json array + */ + public static JsArray parseYaml(final String str) { + + try (JsonParser parser = JacksonFactory.YAML_FACTORY.createParser(requireNonNull(str))) { + final JsonToken keyEvent = parser.nextToken(); + if (START_ARRAY != keyEvent) throw MalformedJson.expectedArray(str); + return new JsArray(parse(parser + )); + } catch (IOException e) { + + throw new MalformedJson(e.getMessage()); + } + + } + /** Tries to parse the string into an immutable json array. diff --git a/src/main/java/jsonvalues/JsObj.java b/src/main/java/jsonvalues/JsObj.java index c9009b7c..dec3dfae 100644 --- a/src/main/java/jsonvalues/JsObj.java +++ b/src/main/java/jsonvalues/JsObj.java @@ -1077,6 +1077,24 @@ public static JsObj parse(final String str) { } } + /** + Tries to parse a YAML string into an immutable object. + + @param str the YAML to be parsed + @return a JsOb object + @throws MalformedJson if the string doesnt represent a json object + */ + public static JsObj parseYaml(final String str) { + + try (JsonParser parser = JacksonFactory.YAML_FACTORY.createParser(requireNonNull(str))) { + JsonToken keyEvent = parser.nextToken(); + if (START_OBJECT != keyEvent) throw MalformedJson.expectedObj(str); + return new JsObj(JsObj.parse(parser)); + } catch (IOException e) { + throw new MalformedJson(e.getMessage()); + } + } + static Map parse(final JsonParser parser) throws IOException { Map map = LinkedHashMap.empty(); String key = parser.nextFieldName(); diff --git a/src/test/java/jsonvalues/TestJsObjParser.java b/src/test/java/jsonvalues/TestJsObjParser.java index a7ff18d1..4f2605b4 100644 --- a/src/test/java/jsonvalues/TestJsObjParser.java +++ b/src/test/java/jsonvalues/TestJsObjParser.java @@ -1235,4 +1235,71 @@ public void test() { } + + @Test + public void testYamlToObj(){ + //language=yaml + String yaml = "Resources:\n" + + " Ec2Instance:\n" + + " Type: 'AWS::EC2::Instance'\n" + + " Properties:\n" + + " SecurityGroups:\n" + + " - !Ref InstanceSecurityGroup\n" + + " KeyName: mykey\n" + + " ImageId: ''\n" + + " InstanceSecurityGroup:\n" + + " Type: 'AWS::EC2::SecurityGroup'\n" + + " Properties:\n" + + " GroupDescription: Enable SSH access via port 22\n" + + " SecurityGroupIngress:\n" + + " - IpProtocol: tcp\n" + + " FromPort: '22'\n" + + " ToPort: 22\n" + + " CidrIp: 0.0.0.0/0"; + + JsObj obj = JsObj.parseYaml(yaml); + + Assertions.assertEquals("AWS::EC2::SecurityGroup", + obj.getStr(JsPath.path("/Resources/InstanceSecurityGroup/Type"))); + Assertions.assertEquals("InstanceSecurityGroup", + obj.getStr(JsPath.path("/Resources/Ec2Instance/Properties/SecurityGroups/0"))); + Assertions.assertEquals("0.0.0.0/0", + obj.getStr(JsPath.path("/Resources/InstanceSecurityGroup/Properties/SecurityGroupIngress/0/CidrIp"))); + + Assertions.assertEquals(new Integer(22), + obj.getInt(JsPath.path("/Resources/InstanceSecurityGroup/Properties/SecurityGroupIngress/0/ToPort"))); + } + + @Test + public void testYamlToArray(){ + String yaml = "- Resources:\n" + + " Ec2Instance:\n" + + " Type: 'AWS::EC2::Instance'\n" + + " Properties:\n" + + " SecurityGroups:\n" + + " - InstanceSecurityGroup\n" + + " KeyName: mykey\n" + + " ImageId: ''\n" + + " InstanceSecurityGroup:\n" + + " Type: 'AWS::EC2::SecurityGroup'\n" + + " Properties:\n" + + " GroupDescription: Enable SSH access via port 22\n" + + " SecurityGroupIngress:\n" + + " - IpProtocol: tcp\n" + + " FromPort: '22'\n" + + " ToPort: 22\n" + + " CidrIp: 0.0.0.0/0"; + + + JsArray arr = JsArray.parseYaml(yaml); + + Assertions.assertEquals("AWS::EC2::SecurityGroup", + arr.getStr(JsPath.path("/0/Resources/InstanceSecurityGroup/Type"))); + Assertions.assertEquals("InstanceSecurityGroup", + arr.getStr(JsPath.path("/0/Resources/Ec2Instance/Properties/SecurityGroups/0"))); + Assertions.assertEquals("0.0.0.0/0", + arr.getStr(JsPath.path("/0/Resources/InstanceSecurityGroup/Properties/SecurityGroupIngress/0/CidrIp"))); + Assertions.assertEquals(new Integer(22), + arr.getInt(JsPath.path("/0/Resources/InstanceSecurityGroup/Properties/SecurityGroupIngress/0/ToPort"))); + } }