001/* 002 * Portions of this software was developed by employees of the National Institute 003 * of Standards and Technology (NIST), an agency of the Federal Government and is 004 * being made available as a public service. Pursuant to title 17 United States 005 * Code Section 105, works of NIST employees are not subject to copyright 006 * protection in the United States. This software may be subject to foreign 007 * copyright. Permission in the United States and in foreign countries, to the 008 * extent that NIST may hold copyright, to use, copy, modify, create derivative 009 * works, and distribute this software and its documentation without fee is hereby 010 * granted on a non-exclusive basis, provided that this notice and disclaimer 011 * of warranty appears in all copies. 012 * 013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER 014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY 015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF 016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM 017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE 018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT 019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, 020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, 021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, 022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR 023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT 024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. 025 */ 026 027package gov.nist.secauto.metaschema.databind.io.yaml; 028 029import org.json.JSONException; 030import org.json.JSONObject; 031import org.yaml.snakeyaml.DumperOptions; 032import org.yaml.snakeyaml.LoaderOptions; 033import org.yaml.snakeyaml.Yaml; 034import org.yaml.snakeyaml.constructor.Constructor; 035import org.yaml.snakeyaml.nodes.Tag; 036import org.yaml.snakeyaml.representer.Representer; 037import org.yaml.snakeyaml.resolver.Resolver; 038 039import java.io.BufferedReader; 040import java.io.IOException; 041import java.nio.charset.StandardCharsets; 042import java.nio.file.Files; 043import java.nio.file.Path; 044import java.util.Map; 045 046import edu.umd.cs.findbugs.annotations.NonNull; 047 048public final class YamlOperations { 049 private static final Yaml YAML_PARSER; 050 051 static { 052 LoaderOptions loaderOptions = new LoaderOptions(); 053 loaderOptions.setCodePointLimit(Integer.MAX_VALUE - 1); // 2GB 054 Constructor constructor = new Constructor(loaderOptions); 055 DumperOptions dumperOptions = new DumperOptions(); 056 Representer representer = new Representer(dumperOptions); 057 YAML_PARSER = new Yaml(constructor, representer, dumperOptions, loaderOptions, new Resolver() { 058 @Override 059 protected void addImplicitResolvers() { 060 addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO"); 061 addImplicitResolver(Tag.INT, INT, "-+0123456789"); 062 addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789."); 063 addImplicitResolver(Tag.MERGE, MERGE, "<"); 064 addImplicitResolver(Tag.NULL, NULL, "~nN\0"); 065 addImplicitResolver(Tag.NULL, EMPTY, null); 066 // addImplicitResolver(Tag.TIMESTAMP, TIMESTAMP, "0123456789"); 067 } 068 }); 069 } 070 071 private YamlOperations() { 072 // disable construction 073 } 074 075 /** 076 * Parse the data represented in YAML in the provided {@code target}, producing 077 * an mapping of field names to Java object values. 078 * 079 * @param target 080 * the YAML file to parse 081 * @return the mapping of field names to Java object values 082 * @throws IOException 083 * if an error occurred while parsing the YAML content 084 */ 085 @SuppressWarnings({ "unchecked", "null" }) 086 @NonNull 087 public static Map<String, Object> parseYaml(Path target) throws IOException { 088 try (BufferedReader reader = Files.newBufferedReader(target.toAbsolutePath(), StandardCharsets.UTF_8)) { 089 return (Map<String, Object>) YAML_PARSER.load(reader); 090 } 091 } 092 093 /** 094 * Converts the provided YAML {@code map} into JSON. 095 * 096 * @param map 097 * the YAML map 098 * @return the JSON object 099 * @throws JSONException 100 * if an error occurred while building the JSON tree 101 */ 102 public static JSONObject yamlToJson(@NonNull Map<String, Object> map) { 103 return new JSONObject(map); 104 } 105}