Skip to content

Commit fb77aa6

Browse files
committed
Add JSON support to AAS generator
Signed-off-by: Johannes Kristan <[email protected]>
1 parent d0127a8 commit fb77aa6

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

core/esmf-aspect-model-aas-generator/src/main/java/org/eclipse/esmf/aspectmodel/aas/AspectModelAASGenerator.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import java.util.function.Function;
1919

2020
import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException;
21+
import org.eclipse.digitaltwin.aas4j.v3.dataformat.Serializer;
2122
import org.eclipse.digitaltwin.aas4j.v3.dataformat.aasx.AASXSerializer;
23+
import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer;
2224
import org.eclipse.digitaltwin.aas4j.v3.dataformat.xml.XmlSerializer;
2325
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
2426

@@ -44,7 +46,7 @@ public void generateAASXFile( final Aspect aspect, final Function<String, Output
4446
/**
4547
* Generates an AAS XML archive file for a given Aspect and writes it to a given OutputStream provided by <code>nameMapper<code/>
4648
*
47-
* @param aspect the Aspect for which an AASX archive shall be generated
49+
* @param aspect the Aspect for which an xml file shall be generated
4850
* @param nameMapper a Name Mapper implementation, which provides an OutputStream for a given filename
4951
* @throws IOException in case the generation can not properly be executed
5052
*/
@@ -55,6 +57,20 @@ public void generateAasXmlFile(
5557
}
5658
}
5759

60+
/**
61+
* Generates an AAS JSON file for a given Aspect and writes it to a given OutputStream provided by <code>nameMapper<code/>
62+
*
63+
* @param aspect the Aspect for which an JSON shall be generated
64+
* @param nameMapper a Name Mapper implementation, which provides an OutputStream for a given filename
65+
* @throws IOException in case the generation can not properly be executed
66+
*/
67+
public void generateAasJsonFile(
68+
final Aspect aspect, final Function<String, OutputStream> nameMapper ) throws IOException {
69+
try ( final OutputStream output = nameMapper.apply( aspect.getName() ) ) {
70+
output.write( generateJsonOutput( aspect ).toByteArray() );
71+
}
72+
}
73+
5874
protected ByteArrayOutputStream generateAasxOutput( Aspect aspect ) throws IOException {
5975
final AspectModelAASVisitor visitor = new AspectModelAASVisitor();
6076
Environment environment = visitor.visitAspect( aspect, null );
@@ -69,11 +85,18 @@ protected ByteArrayOutputStream generateAasxOutput( Aspect aspect ) throws IOExc
6985
}
7086

7187
protected ByteArrayOutputStream generateXmlOutput( Aspect aspect ) throws IOException {
88+
return generate( new XmlSerializer(), aspect );
89+
}
90+
91+
protected ByteArrayOutputStream generateJsonOutput( Aspect aspect ) throws IOException {
92+
return generate( new JsonSerializer(), aspect );
93+
}
94+
95+
protected ByteArrayOutputStream generate( Serializer serializer, Aspect aspect ) throws IOException {
7296
final AspectModelAASVisitor visitor = new AspectModelAASVisitor();
7397
Environment environment = visitor.visitAspect( aspect, null );
7498

7599
try ( ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
76-
XmlSerializer serializer = new XmlSerializer();
77100
serializer.write( out, environment );
78101
return out;
79102
} catch ( SerializationException e ) {

tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/to/AspectToAasCommand.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class AspectToAasCommand extends AbstractCommand {
3535
public static final String COMMAND_NAME = "aas";
3636
public static final String AASX = "aasx";
3737
public static final String XML = "xml";
38+
public static final String JSON = "json";
39+
3840

3941
@CommandLine.Option(
4042
names = { "--output", "-o" },
@@ -43,7 +45,7 @@ public class AspectToAasCommand extends AbstractCommand {
4345

4446
@CommandLine.Option(
4547
names = { "--format", "-f" },
46-
description = "The file format the AAS is to be generated. Valid options are \"" + AASX + "\" and \"" + XML + "\". Default is \"" + XML + "\"." )
48+
description = "The file format the AAS is to be generated. Valid options are \"" + AASX + "\", \"" + JSON + "\", and \"" + XML + "\". Default is \"" + XML + "\"." )
4749
private String format = XML;
4850

4951
@CommandLine.ParentCommand
@@ -63,9 +65,14 @@ public void run() {
6365
// we intentionally override the name of the generated artifact here to the name explicitly
6466
// desired by the user (outputFilePath), as opposed to what the model thinks it should be
6567
// called (name)
66-
if ( format.equals( AASX ) ) {
68+
switch ( format ) {
69+
case AASX:
6770
generator.generateAASXFile( aspect, name -> getStreamForFile( outputFilePath ) );
68-
} else {
71+
break;
72+
case JSON:
73+
generator.generateAasJsonFile( aspect, name -> getStreamForFile( outputFilePath ) );
74+
break;
75+
default:
6976
generator.generateAasXmlFile( aspect, name -> getStreamForFile( outputFilePath ) );
7077
}
7178
} catch ( final IOException e ) {

0 commit comments

Comments
 (0)