开发者

Is there a good yaml library for Android?

Is there a java yaml li开发者_JAVA技巧brary as good as snakeyaml for Android? (Or is anyone successfully using snakeyaml on Android already?)


I do not think you can use SnakeYaml on Android without modifications (at least now).

By default, SnakeYaml uses Introspector to get PropertyDescriptors for classes, and as I can see java.beans.Introspector is not available on Android. But there is BeanAccess.FEILD mode in the SnakeYaml which uses fields to dump/load beans. That mode uses only java.lang.reflect classes available on Android.

So, with some modifications it might work. But I need to try it to be sure.

Added

Now android compatible version of SnakeYaml can be build using :

mvn -Pandroid clean package

Update (March 2017):

Starting from 1.18 android build is in central. You can add dependency into your pom.xml like this

<dependency>
  <groupId>org.yaml</groupId>
  <artifactId>snakeyaml</artifactId>
  <version> VERSION </version>
  <classifier>android</classifier>
</dependency>

or in your build.gradle like this:

implementation "org.yaml:snakeyaml:1.18:android"


I think you should at least try directly snakeyaml. For this kind of library it's highly probable that the needed Java API set is available in Android.

If it does not work, you may try to import snakeyaml source code in an android project and see what doesn't compile. With a little luck it will be possible to work around the missing APIs.


From the answer How do I convert from YAML to JSON in Java?

String convertYamlToJson(String yaml) {
    ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
    Object obj = yamlReader.readValue(yaml, Object.class);

    ObjectMapper jsonWriter = new ObjectMapper();
    return jsonWriter.writeValueAsString(obj);
}

Requires:

implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.7.4'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜