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.jsonl;
018
019import java.util.*;
020
021import org.apache.juneau.marshall.collections.*;
022import org.apache.juneau.marshall.json.*;
023
024/**
025 * Parses JSONL (JSON Lines) input into POJO models.
026 *
027 * <h2 class='topic'>Media types</h2>
028 * <p>
029 * Handles <c>Content-Type</c> types:  <bc>application/jsonl, application/jsonlines, application/x-ndjson, text/jsonl</bc>
030 *
031 * <h2 class='topic'>Description</h2>
032 * <p>
033 * Parses JSONL input where each non-empty line is a complete JSON value.  Blank lines are ignored.
034 * The conversion is as follows...
035 * <ul class='spaced-list'>
036 *    <li>JSON objects (<c>{...}</c>) are converted to {@link JsonMap JsonMaps}
037 *       or Java beans if a target type is provided.
038 *    <li>JSON arrays (<c>[...]</c>) are converted to {@link JsonList JsonLists}.
039 *    <li>JSON strings are converted to {@link String Strings}.
040 *    <li>JSON numbers are converted to {@link Integer Integers}, {@link Long Longs},
041 *       {@link Float Floats}, or {@link Double Doubles}.
042 *    <li>JSON booleans are converted to {@link Boolean Booleans}.
043 *    <li>JSON nulls are converted to <jk>null</jk>.
044 * </ul>
045 * <p>
046 * When the target type is a {@link Collection} or array, each line is parsed
047 * as one element.  When the target type is a single value, only the first non-empty line is parsed.
048 *
049 * <h2 class='section'>Example:</h2>
050 * <p class='bjava'>
051 *    <jc>// Use the default parser to parse JSONL into a list of POJOs</jc>
052 *    List&lt;MyBean&gt; <jv>list</jv> = JsonlParser.<jsf>DEFAULT</jsf>.read(<jv>jsonlInput</jv>, List.<jk>class</jk>, MyBean.<jk>class</jk>);
053 *
054 *    <jc>// Create a custom parser</jc>
055 *    JsonlParser <jv>parser</jv> = JsonlParser.<jsm>create</jsm>().build();
056 *
057 *    <jc>// Clone an existing parser</jc>
058 *    <jv>parser</jv> = JsonlParser.<jsf>DEFAULT</jsf>.copy().build();
059 * </p>
060 *
061 * <h2 class='figure'>Example input:</h2>
062 * <p class='bjson'>
063 * {"name":"Alice","age":30}
064 * {"name":"Bob","age":25}
065 * {"name":"Carol","age":35}
066 * </p>
067 *
068 * <h2 class='section'>Limitations compared to JSON</h2>
069 * <ul class='spaced-list'>
070 *    <li><b>No multi-line JSON input</b>: Each JSON value must be on a single line.
071 *       Multi-line (pretty-printed) JSON piped as JSONL input will cause parse errors.
072 *    <li><b>First-line-only for non-collection targets</b>: When parsing JSONL to a single
073 *       non-collection type, only the first non-empty line is parsed; trailing lines are
074 *       silently ignored.
075 * </ul>
076 *
077 * <h2 class='section'>Notes:</h2><ul>
078 *    <li class='note'>This class is thread safe and reusable.
079 * </ul>
080 *
081 * <h2 class='section'>See Also:</h2><ul>
082 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JsonlSupport">JSONL</a>
083 * </ul>
084 */
085@SuppressWarnings({
086   "java:S110", // Inheritance depth acceptable
087   "java:S9149" // Per-format static factories intentionally shadow the parent's.
088})
089public class JsonlParser extends JsonParser {
090
091   /** Default parser. */
092   public static final JsonlParser DEFAULT = new JsonlParser(create());
093
094   /**
095    * Creates a new builder for this object.
096    *
097    * @return A new builder.
098    */
099   @SuppressWarnings({
100      "java:S1452" // Builder<?> wildcard return intentional; callers chain via fluent API without needing the concrete type
101   })
102   public static JsonParser.Builder<?> create() {
103      return JsonParser.create()
104         .consumes("application/jsonl,application/jsonlines,application/x-ndjson,text/jsonl")
105         .type(JsonlParser.class);
106   }
107
108   /**
109    * Constructor.
110    *
111    * @param builder The builder for this object.
112    */
113   public JsonlParser(JsonParser.Builder<?> builder) {
114      super(builder);
115   }
116
117   @Override /* Overridden from Context */
118   public JsonlParserSession.Builder createSession() {
119      return JsonlParserSession.create(this);
120   }
121
122}