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