001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.dto.jsonschema;
014
015/**
016 * Represents possible JSON types in the JSON-Schema core specification.
017 *
018 * <p>
019 * Implements custom <c>toString()</c> and <c>fromString(String)</c> methods that override the default
020 * serialization/parsing behavior of <c>Enum</c> types so that they are represented in lowercase form (as per the
021 * specification).
022 *
023 * <h5 class='section'>Example:</h5>
024 * <p class='bjava'>
025 *    <jc>// Produces 'number', not 'NUMBER'.</jc>
026 *    String <jv>json</jv> = JsonSerializer.<jsf>DEFAULT</jsf>.serialize(JsonType.<jsf>NUMBER</jsf>);
027 * </p>
028 */
029public enum JsonType {
030
031   /** array */
032   ARRAY("array"),
033
034   /** boolean */
035   BOOLEAN("boolean"),
036
037   /** integer */
038   INTEGER("integer"),
039
040   /** null */
041   NULL("null"),
042
043   /** number */
044   NUMBER("number"),
045
046   /** object */
047   OBJECT("object"),
048
049   /** string */
050   STRING("string"),
051
052   /** any */
053   ANY("any");
054
055   private final String value;   // The serialized format of the enum.
056
057   private JsonType(String value) {
058      this.value = value;
059   }
060
061   /**
062    * Returns the lowercase form of this enum that's compatible with the JSON-Schema specification.
063    */
064   @Override /* Object */
065   public String toString() {
066      return value;
067   }
068
069   /**
070    * Converts the specified lowercase form of the enum back into an <c>Enum</c>.
071    *
072    * @param value The lowercase form of the enum (e.g. <js>"array"</js>).
073    * @return The matching <c>Enum</c>, or <jk>null</jk> if no match found.
074    */
075   public static JsonType fromString(String value) {
076      if (value == null || value.length() < 4)
077         return null;
078      char c = value.charAt(0);
079      if (c == 'a') {
080         if (value.equals("array"))
081         return ARRAY;
082         if (value.equals("any"))
083            return ANY;
084      }
085      if (c == 'b' && value.equals("boolean"))
086         return BOOLEAN;
087      if (c == 'i' && value.equals("integer"))
088         return INTEGER;
089      if (c == 'n') {
090         c = value.charAt(2);
091         if (c == 'l' && value.equals("null"))
092            return NULL;
093         if (c == 'm' && value.equals("number"))
094            return NUMBER;
095         return null;
096      }
097      if (c == 'o' && value.equals("object"))
098         return OBJECT;
099      if (c == 's' && value.equals("string"))
100         return STRING;
101      return null;
102   }
103}