Serenity Operating System
at portability 115 lines 3.1 kB view raw
1/* 2 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@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#pragma once 28 29#include <AK/JsonValue.h> 30 31namespace AK { 32 33template<typename Builder> 34class JsonObjectSerializer; 35 36template<typename Builder> 37class JsonArraySerializer { 38public: 39 explicit JsonArraySerializer(Builder& builder) 40 : m_builder(builder) 41 { 42 m_builder.append('['); 43 } 44 45 JsonArraySerializer(const JsonArraySerializer&) = delete; 46 JsonArraySerializer(JsonArraySerializer&&) = delete; 47 48 ~JsonArraySerializer() 49 { 50 if (!m_finished) 51 finish(); 52 } 53 54 void add(const JsonValue& value) 55 { 56 begin_item(); 57 value.serialize(m_builder); 58 } 59 60 void add(const StringView& value) 61 { 62 begin_item(); 63 m_builder.append('"'); 64 m_builder.append(value); 65 m_builder.append('"'); 66 } 67 68 void add(const String& value) 69 { 70 begin_item(); 71 m_builder.append('"'); 72 m_builder.append(value); 73 m_builder.append('"'); 74 } 75 76 void add(const char* value) 77 { 78 begin_item(); 79 m_builder.append('"'); 80 m_builder.append(value); 81 m_builder.append('"'); 82 } 83 84 JsonArraySerializer<Builder> add_array() 85 { 86 begin_item(); 87 return JsonArraySerializer(m_builder); 88 } 89 90 // Implemented in JsonObjectSerializer.h 91 JsonObjectSerializer<Builder> add_object(); 92 93 void finish() 94 { 95 ASSERT(!m_finished); 96 m_finished = true; 97 m_builder.append(']'); 98 } 99 100private: 101 void begin_item() 102 { 103 if (!m_empty) 104 m_builder.append(','); 105 m_empty = false; 106 } 107 108 Builder& m_builder; 109 bool m_empty { true }; 110 bool m_finished { false }; 111}; 112 113} 114 115using AK::JsonArraySerializer;