Skip to content

Commit 8241c35

Browse files
authoredMay 13, 2022
implement yaml2json and json2yaml (#3)
1 parent c0163a1 commit 8241c35

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed
 

‎cli/yaml2json/build.gradle

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2+
import org.graalvm.buildtools.gradle.dsl.NativeImageOptions
3+
4+
plugins {
5+
id 'info.ankin.projects.app-conventions'
6+
}
7+
8+
version = '0.0.1'
9+
10+
dependencies {
11+
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.2'
12+
}
13+
14+
// for shadowJar
15+
application.mainClass.set 'info.ankin.projects.cli.yaml2json.Yaml2Json'
16+
17+
shadowJar {
18+
// output first jar as "shadow"
19+
// so as not to confuse with yaml2json and json2Yaml
20+
archiveBaseName.set 'shadow'
21+
}
22+
23+
void customShadowJar(ShadowJar s, String name, String className) {
24+
s.group 'shadow'
25+
s.description name + 'Jar shadow jar'
26+
s.archiveBaseName.set name
27+
s.manifest {
28+
attributes('Main-Class': 'info.ankin.projects.cli.yaml2json.' + className)
29+
}
30+
s.from zipTree(tasks.shadowJar.getArchiveFile())
31+
}
32+
33+
def yaml2JsonJar = tasks.register('yaml2JsonJar', ShadowJar) {
34+
customShadowJar(it, 'yaml2Json', 'Yaml2Json')
35+
}
36+
37+
def json2YamlJar = tasks.register('json2YamlJar', ShadowJar) {
38+
customShadowJar(it, 'json2Yaml', 'Json2Yaml')
39+
}
40+
41+
tasks {
42+
shadowJar {
43+
finalizedBy yaml2JsonJar, json2YamlJar
44+
}
45+
}
46+
47+
task example {
48+
doLast {
49+
println
50+
}
51+
}
52+
53+
// for graalvmNative
54+
tasks.jar.enabled(true)
55+
56+
// https://graalvm.github.io/native-build-tools/0.9.11/gradle-plugin.html#_configuration_options
57+
graalvmNative {
58+
binaries {
59+
json2yaml { NativeImageOptions o ->
60+
o.imageName.set 'json2yaml'
61+
o.mainClass.set json2YamlJar.get().getManifest().getAttributes().get('Main-Class')
62+
63+
o.debug.set false
64+
o.verbose.set true
65+
o.fallback.set false
66+
o.buildArgs.add('-Ob') // faster development builds
67+
o.useFatJar.set false
68+
o.classpath sourceSets.main.output.files, project.tasks.shadowJar
69+
}
70+
71+
yaml2json { NativeImageOptions o ->
72+
o.imageName.set 'yaml2json'
73+
o.mainClass.set yaml2JsonJar.get().getManifest().getAttributes().get('Main-Class')
74+
o.debug.set false
75+
o.verbose.set true
76+
o.fallback.set false
77+
o.buildArgs.add('-Ob') // faster development builds
78+
o.useFatJar.set false
79+
o.classpath sourceSets.main.output.files, project.tasks.shadowJar
80+
}
81+
}
82+
}
83+
84+
tasks {
85+
nativeCompile {
86+
dependsOn nativeYaml2jsonCompile, nativeJson2yamlCompile
87+
doLast {
88+
copy {
89+
from tasks.nativeJson2yamlCompile.outputs.files.files
90+
from tasks.nativeYaml2jsonCompile.outputs.files.files
91+
92+
into tasks.nativeCompile.outputs.files.singleFile
93+
}
94+
}
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package info.ankin.projects.cli.yaml2json;
2+
3+
import com.fasterxml.jackson.core.JsonGenerator;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.json.JsonMapper;
6+
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
7+
import lombok.SneakyThrows;
8+
9+
public class Json2Yaml {
10+
@SneakyThrows
11+
public static void main(String[] args) {
12+
JsonMapper jsonMapper = new JsonMapper();
13+
YAMLMapper yamlMapper = new YAMLMapper();
14+
yamlMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
15+
16+
JsonNode jsonNode = jsonMapper.reader().readTree(System.in);
17+
yamlMapper.writeValue(System.out, jsonNode);
18+
System.out.println();
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package info.ankin.projects.cli.yaml2json;
2+
3+
import com.fasterxml.jackson.core.JsonGenerator;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.json.JsonMapper;
6+
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
7+
import lombok.SneakyThrows;
8+
9+
public class Yaml2Json {
10+
@SneakyThrows
11+
public static void main(String[] args) {
12+
JsonMapper jsonMapper = new JsonMapper();
13+
jsonMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
14+
YAMLMapper yamlMapper = new YAMLMapper();
15+
16+
JsonNode jsonNode = yamlMapper.reader().readTree(System.in);
17+
jsonMapper.writeValue(System.out, jsonNode);
18+
System.out.println();
19+
}
20+
}

‎settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
rootProject.name = 'java-projects'
22
include 'cli:killport'
3+
include 'cli:yaml2json'
34
include 'system:infer-platform'
45
include 'system:terminal:supports-hyperlinks'

0 commit comments

Comments
 (0)
Please sign in to comment.