Serenity Operating System
at portability 199 lines 4.9 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/Function.h> 28#include <AK/JsonArray.h> 29#include <AK/JsonObject.h> 30#include <AK/JsonParser.h> 31#include <AK/JsonValue.h> 32#include <AK/StringBuilder.h> 33 34namespace AK { 35 36JsonValue::JsonValue(Type type) 37 : m_type(type) 38{ 39} 40 41JsonValue::JsonValue(const JsonValue& other) 42{ 43 copy_from(other); 44} 45 46JsonValue& JsonValue::operator=(const JsonValue& other) 47{ 48 if (this != &other) { 49 clear(); 50 copy_from(other); 51 } 52 return *this; 53} 54 55void JsonValue::copy_from(const JsonValue& other) 56{ 57 m_type = other.m_type; 58 switch (m_type) { 59 case Type::String: 60 ASSERT(!m_value.as_string); 61 m_value.as_string = other.m_value.as_string; 62 m_value.as_string->ref(); 63 break; 64 case Type::Object: 65 m_value.as_object = new JsonObject(*other.m_value.as_object); 66 break; 67 case Type::Array: 68 m_value.as_array = new JsonArray(*other.m_value.as_array); 69 break; 70 default: 71 m_value.as_string = other.m_value.as_string; 72 break; 73 } 74} 75 76JsonValue::JsonValue(JsonValue&& other) 77{ 78 m_type = exchange(other.m_type, Type::Undefined); 79 m_value.as_string = exchange(other.m_value.as_string, nullptr); 80} 81 82JsonValue& JsonValue::operator=(JsonValue&& other) 83{ 84 if (this != &other) { 85 clear(); 86 m_type = exchange(other.m_type, Type::Undefined); 87 m_value.as_string = exchange(other.m_value.as_string, nullptr); 88 } 89 return *this; 90} 91 92JsonValue::JsonValue(i32 value) 93 : m_type(Type::Int32) 94{ 95 m_value.as_i32 = value; 96} 97 98JsonValue::JsonValue(u32 value) 99 : m_type(Type::UnsignedInt32) 100{ 101 m_value.as_u32 = value; 102} 103 104JsonValue::JsonValue(i64 value) 105 : m_type(Type::Int64) 106{ 107 m_value.as_i64 = value; 108} 109 110JsonValue::JsonValue(u64 value) 111 : m_type(Type::UnsignedInt64) 112{ 113 m_value.as_u64 = value; 114} 115 116JsonValue::JsonValue(const char* cstring) 117 : JsonValue(String(cstring)) 118{ 119} 120 121#if !defined(BOOTSTRAPPER) && !defined(KERNEL) 122JsonValue::JsonValue(double value) 123 : m_type(Type::Double) 124{ 125 m_value.as_double = value; 126} 127#endif 128 129JsonValue::JsonValue(bool value) 130 : m_type(Type::Bool) 131{ 132 m_value.as_bool = value; 133} 134 135JsonValue::JsonValue(const String& value) 136{ 137 if (value.is_null()) { 138 m_type = Type::Null; 139 } else { 140 m_type = Type::String; 141 m_value.as_string = const_cast<StringImpl*>(value.impl()); 142 m_value.as_string->ref(); 143 } 144} 145 146JsonValue::JsonValue(const IPv4Address& value) 147 : JsonValue(value.to_string()) 148{ 149} 150 151JsonValue::JsonValue(const JsonObject& value) 152 : m_type(Type::Object) 153{ 154 m_value.as_object = new JsonObject(value); 155} 156 157JsonValue::JsonValue(const JsonArray& value) 158 : m_type(Type::Array) 159{ 160 m_value.as_array = new JsonArray(value); 161} 162 163JsonValue::JsonValue(JsonObject&& value) 164 : m_type(Type::Object) 165{ 166 m_value.as_object = new JsonObject(move(value)); 167} 168 169JsonValue::JsonValue(JsonArray&& value) 170 : m_type(Type::Array) 171{ 172 m_value.as_array = new JsonArray(move(value)); 173} 174 175void JsonValue::clear() 176{ 177 switch (m_type) { 178 case Type::String: 179 m_value.as_string->unref(); 180 break; 181 case Type::Object: 182 delete m_value.as_object; 183 break; 184 case Type::Array: 185 delete m_value.as_array; 186 break; 187 default: 188 break; 189 } 190 m_type = Type::Undefined; 191 m_value.as_string = nullptr; 192} 193 194JsonValue JsonValue::from_string(const StringView& input) 195{ 196 return JsonParser(input).parse(); 197} 198 199}