Skip to content

Commit

Permalink
9.0.0 rc3
Browse files Browse the repository at this point in the history
* 9.0.0 rc2 (#162)

* feat: ๐ŸŽธ streamValues methods

* ci: ๐ŸŽก 9.0.0-RC2

* feat: ๐ŸŽธ parse yaml to json

* feat: ๐ŸŽธ parse yaml to json
  • Loading branch information
imrafaelmerino committed Jan 23, 2021
1 parent 68bfc06 commit e35646c
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 8 deletions.
5 changes: 3 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -170,7 +171,7 @@ Add the following dependency to your building tool:
<dependency>
<groupId>com.github.imrafaelmerino</groupId>
<artifactId>json-values</artifactId>
<version>9.0.0-RC2</version>
<version>9.0.0-RC3</version>
</dependency>
```

Expand Down
17 changes: 12 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
<groupId>com.github.imrafaelmerino</groupId>
<artifactId>json-values</artifactId>
<packaging>jar</packaging>
<version>9.0.0-RC2</version>

<name>json-values</name>
<version>9.0.0-RC3</version>
<name>json-values</name>
<description>json-values is a functional Java library to work with immutable jsons using persistent data structures.</description>
<url>https://github.com/imrafaelmerino/json-values</url>
<licenses>
Expand Down Expand Up @@ -43,13 +42,14 @@
<MAVEN-SUREFIRE-PLUGIN.VERSION>2.22.0</MAVEN-SUREFIRE-PLUGIN.VERSION>
<MY-DSL-JSON.VERSION>0.1</MY-DSL-JSON.VERSION>
<DSL-JSON.VERSION>1.9.7</DSL-JSON.VERSION>
<vavr.version>0.10.3</vavr.version>
<VAVR.VERSION>0.10.3</VAVR.VERSION>
<JACKSON-DATAFORMAT-YAML.VERSION>2.9.5</JACKSON-DATAFORMAT-YAML.VERSION>
</properties>
<dependencies>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>${vavr.version}</version>
<version>${VAVR.VERSION}</version>
</dependency>

<dependency>
Expand All @@ -70,12 +70,19 @@
<version>${JACKSON-VERSION}</version>
</dependency>



<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${JACKSON-DATAFORMAT-YAML.VERSION}</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/jsonvalues/JacksonFactory.java
Original file line number Diff line number Diff line change
@@ -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() {
}
}
21 changes: 21 additions & 0 deletions src/main/java/jsonvalues/JsArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,27 @@ public static JsArray ofIterable(Iterable<? extends JsValue> 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.
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/jsonvalues/JsObj.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, JsValue> parse(final JsonParser parser) throws IOException {
Map<String, JsValue> map = LinkedHashMap.empty();
String key = parser.nextFieldName();
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/jsonvalues/TestJsObjParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
}
}

0 comments on commit e35646c

Please sign in to comment.