001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.marshall;
018
019import java.io.*;
020
021import org.apache.juneau.commons.bean.*;
022import org.apache.juneau.marshall.collections.*;
023import org.apache.juneau.marshall.json5.*;
024import org.apache.juneau.marshall.parser.*;
025
026/**
027 * Marshalling-side helper for loading {@link BeanMap} contents from JSON text or a reader/parser pair.
028 *
029 * <h5 class='section'>Example:</h5>
030 * <p class='bjava'>
031 *    <jc>// Populate a bean map from a JSON string.</jc>
032 *    BeanMap&lt;Person&gt; <jv>m</jv> = <jv>bc</jv>.newBeanMap(Person.<jk>class</jk>);
033 *    BeanMapLoader.<jsm>load</jsm>(<jv>m</jv>, <js>"{name:'John',age:21}"</js>);
034 * </p>
035 */
036public final class BeanMapLoader {
037
038   private BeanMapLoader() {}
039
040   /**
041    * Populates the supplied {@link BeanMap} with the contents of the JSON text in {@code input}.
042    *
043    * <p>
044    * Equivalent to the legacy {@code BeanMap.load(String)} method.
045    *
046    * @param <T> The bean type.
047    * @param m The bean map to populate. Must not be <jk>null</jk>.
048    * @param input The text that will get parsed into a map and then added to {@code m}. Can be <jk>null</jk> (nothing is added).
049    * @return The supplied bean map for fluent chaining.
050    * @throws ParseException Malformed input encountered.
051    */
052   public static <T> BeanMap<T> load(BeanMap<T> m, String input) throws ParseException {
053      m.putAll(Json5Map.ofString(input));
054      return m;
055   }
056
057   /**
058    * Populates the supplied {@link BeanMap} with the contents of the reader using the specified {@link ReaderParser}.
059    *
060    * <p>
061    * Equivalent to the legacy {@code BeanMap.load(Reader, ReaderParser)} method.
062    *
063    * @param <T> The bean type.
064    * @param m The bean map to populate. Must not be <jk>null</jk>.
065    * @param r The reader containing serialized text. Can be <jk>null</jk> (nothing is added).
066    * @param p The parser to use to parse the text.
067    * @return The supplied bean map for fluent chaining.
068    * @throws ParseException Malformed input encountered.
069    */
070   public static <T> BeanMap<T> load(BeanMap<T> m, Reader r, ReaderParser p) throws ParseException {
071      m.putAll(JsonMap.ofString(r, p));
072      return m;
073   }
074}