On Fri, Jan 18, 2019 at 5:24 PM Tom Pantelis <[email protected]> wrote:
> > > On Fri, Jan 18, 2019 at 4:13 PM Sam Hague <[email protected]> wrote: > >> Any idea if there is a portable java method that can take json as input >> and output java objects? Or some example code that maybe isn't as generic, >> but with some case-specific coding could do the same? >> >> The use case is for writing unit tests. Every new listener I add tests >> for I always create helper methods to fill out the java objects, write to >> mdsal, do some verifications, write some more and so on. It would be much >> more readable to simply input a json into this new helper method and out >> pop a java object that can be written to mdsal. >> >> I imagine the restconf project has something similar since it parses >> incoming json. Or maybe it is using some libraries to do the same? >> > > restconf doesn't use binding classes - it converts json/xml to > NormalizedNode - this is done in > https://github.com/opendaylight/netconf/blob/master/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/JsonNormalizedNodeBodyReader.java > : > > final NormalizedNodeResult resultHolder = new NormalizedNodeResult(); > final NormalizedNodeStreamWriter writer = > ImmutableNormalizedNodeStreamWriter.from(resultHolder); > ... > final JsonParserStream jsonParser = JsonParserStream.create(writer, > > JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(path.getSchemaContext()), > parentSchema); > final JsonReader reader = new JsonReader(new > InputStreamReader(nonEmptyInputStreamOptional.get(), > StandardCharsets.UTF_8)); > jsonParser.parse(reader); > > NormalizedNode<?, ?> result = resultHolder.getResult(); > > > Then you can convert the NormalizedNode to a binding object > via BindingToNormalizedNodeCodec: > > BindingToNormalizedNodeCodec codec = ... > DataObject javaObject = codec.fromNormalizedNode(path, > result).getValue(); > > Nice, Ok this looks like a good way to do it. I could extract that piece of code and feed it the models I know need to be converted. And that should be pretty lightweight for that piece of code, right? Doesn't look like there are many dependencies just for that part. > >> Thanks, Sam >> _______________________________________________ >> Discuss mailing list >> [email protected] >> https://lists.opendaylight.org/mailman/listinfo/discuss >> >
_______________________________________________ Discuss mailing list [email protected] https://lists.opendaylight.org/mailman/listinfo/discuss
