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='bcode w800'>
025 *    // Produces 'number', not 'NUMBER'.
026 *    String json = JsonSerializer.DEFAULT.serialize(JsonType.NUMBER);
027 * </p>
028 *
029 * <ul class='seealso'>
030 *    <li class='jp'>{@doc package-summary.html#TOC org.apache.juneau.dto.jsonschema}
031 * </ul>
032 */
033public enum JsonType {
034
035   /** array */
036   ARRAY("array"),
037
038   /** boolean */
039   BOOLEAN("boolean"),
040
041   /** integer */
042   INTEGER("integer"),
043
044   /** null */
045   NULL("null"),
046
047   /** number */
048   NUMBER("number"),
049
050   /** object */
051   OBJECT("object"),
052
053   /** string */
054   STRING("string"),
055
056   /** any */
057   ANY("any");
058
059   private final String value;   // The serialized format of the enum.
060
061   private JsonType(String value) {
062      this.value = value;
063   }
064
065   /**
066    * Returns the lowercase form of this enum that's compatible with the JSON-Schema specification.
067    */
068   @Override /* Object */
069   public String toString() {
070      return value;
071   }
072
073   /**
074    * Converts the specified lowercase form of the enum back into an <c>Enum</c>.
075    *
076    * @param value The lowercase form of the enum (e.g. <js>"array"</js>).
077    * @return The matching <c>Enum</c>, or <jk>null</jk> if no match found.
078    */
079   public static JsonType fromString(String value) {
080      if (value == null || value.length() < 4)
081         return null;
082      char c = value.charAt(0);
083      if (c == 'a') {
084         if (value.equals("array"))
085         return ARRAY;
086         if (value.equals("any"))
087            return ANY;
088      }
089      if (c == 'b' && value.equals("boolean"))
090         return BOOLEAN;
091      if (c == 'i' && value.equals("integer"))
092         return INTEGER;
093      if (c == 'n') {
094         c = value.charAt(2);
095         if (c == 'l' && value.equals("null"))
096            return NULL;
097         if (c == 'm' && value.equals("number"))
098            return NUMBER;
099         return null;
100      }
101      if (c == 'o' && value.equals("object"))
102         return OBJECT;
103      if (c == 's' && value.equals("string"))
104         return STRING;
105      return null;
106   }
107}