1 | using Utf8Json;
|
---|
2 |
|
---|
3 | namespace Webserver.WebAPI.APIs.ServerState {
|
---|
4 | public abstract class KeyValueListAbs : AbsRestApi {
|
---|
5 | private readonly UnityEngine.Profiling.CustomSampler buildSampler;
|
---|
6 |
|
---|
7 | private static readonly byte[] keyName = JsonWriter.GetEncodedPropertyNameWithBeginObject ("name");
|
---|
8 | private static readonly byte[] keyType = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("type");
|
---|
9 | private static readonly byte[] keyValue = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("value");
|
---|
10 | private static readonly byte[] keyDefault = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("default");
|
---|
11 |
|
---|
12 | private int largestBuffer;
|
---|
13 |
|
---|
14 | protected KeyValueListAbs (string _listName) {
|
---|
15 | buildSampler = UnityEngine.Profiling.CustomSampler.Create ($"JSON_{_listName}_BuildSampler");
|
---|
16 | }
|
---|
17 |
|
---|
18 | protected override void HandleRestGet (RequestContext _context) {
|
---|
19 | buildSampler.Begin ();
|
---|
20 |
|
---|
21 | PrepareEnvelopedResult (out JsonWriter writer);
|
---|
22 |
|
---|
23 | writer.EnsureCapacity (largestBuffer);
|
---|
24 | writer.WriteBeginArray ();
|
---|
25 |
|
---|
26 | bool first = true;
|
---|
27 | iterateList (ref writer, ref first);
|
---|
28 |
|
---|
29 | writer.WriteEndArray ();
|
---|
30 |
|
---|
31 | buildSampler.End ();
|
---|
32 |
|
---|
33 | int bufferContentSize = writer.CurrentOffset + 128;
|
---|
34 | if (bufferContentSize > largestBuffer) {
|
---|
35 | largestBuffer = bufferContentSize;
|
---|
36 | }
|
---|
37 |
|
---|
38 | SendEnvelopedResult (_context, ref writer);
|
---|
39 | }
|
---|
40 |
|
---|
41 | private void writeKeyType (ref JsonWriter _writer, ref bool _first, string _key, string _type) {
|
---|
42 | if (!_first) {
|
---|
43 | _writer.WriteValueSeparator ();
|
---|
44 | }
|
---|
45 |
|
---|
46 | _first = false;
|
---|
47 |
|
---|
48 | _writer.WriteRaw (keyName);
|
---|
49 | _writer.WriteString (_key);
|
---|
50 |
|
---|
51 | _writer.WriteRaw (keyType);
|
---|
52 | _writer.WriteString (_type);
|
---|
53 | }
|
---|
54 |
|
---|
55 | private void writeInt (ref JsonWriter _writer, ref bool _first, string _key, int _value) {
|
---|
56 | writeKeyType (ref _writer, ref _first, _key, "int");
|
---|
57 | _writer.WriteRaw (keyValue);
|
---|
58 | _writer.WriteInt32 (_value);
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected void addItem (ref JsonWriter _writer, ref bool _first, string _key, int _value) {
|
---|
62 | writeInt (ref _writer, ref _first, _key, _value);
|
---|
63 | _writer.WriteEndObject ();
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected void addItem (ref JsonWriter _writer, ref bool _first, string _key, int _value, int? _default) {
|
---|
67 | writeInt (ref _writer, ref _first, _key, _value);
|
---|
68 |
|
---|
69 | _writer.WriteRaw (keyDefault);
|
---|
70 | if (_default.HasValue) {
|
---|
71 | _writer.WriteInt32 (_default.Value);
|
---|
72 | } else {
|
---|
73 | _writer.WriteNull ();
|
---|
74 | }
|
---|
75 |
|
---|
76 | _writer.WriteEndObject ();
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | private void writeFloat (ref JsonWriter _writer, ref bool _first, string _key, float _value) {
|
---|
81 | writeKeyType (ref _writer, ref _first, _key, "float");
|
---|
82 | _writer.WriteRaw (keyValue);
|
---|
83 | _writer.WriteSingle (_value);
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected void addItem (ref JsonWriter _writer, ref bool _first, string _key, float _value) {
|
---|
87 | writeFloat (ref _writer, ref _first, _key, _value);
|
---|
88 | _writer.WriteEndObject ();
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected void addItem (ref JsonWriter _writer, ref bool _first, string _key, float _value, float? _default) {
|
---|
92 | writeFloat (ref _writer, ref _first, _key, _value);
|
---|
93 |
|
---|
94 | _writer.WriteRaw (keyDefault);
|
---|
95 | if (_default.HasValue) {
|
---|
96 | _writer.WriteSingle (_default.Value);
|
---|
97 | } else {
|
---|
98 | _writer.WriteNull ();
|
---|
99 | }
|
---|
100 |
|
---|
101 | _writer.WriteEndObject ();
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | private void writeBool (ref JsonWriter _writer, ref bool _first, string _key, bool _value) {
|
---|
106 | writeKeyType (ref _writer, ref _first, _key, "bool");
|
---|
107 | _writer.WriteRaw (keyValue);
|
---|
108 | _writer.WriteBoolean (_value);
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected void addItem (ref JsonWriter _writer, ref bool _first, string _key, bool _value) {
|
---|
112 | writeBool (ref _writer, ref _first, _key, _value);
|
---|
113 | _writer.WriteEndObject ();
|
---|
114 | }
|
---|
115 |
|
---|
116 | protected void addItem (ref JsonWriter _writer, ref bool _first, string _key, bool _value, bool? _default) {
|
---|
117 | writeBool (ref _writer, ref _first, _key, _value);
|
---|
118 |
|
---|
119 | _writer.WriteRaw (keyDefault);
|
---|
120 | if (_default.HasValue) {
|
---|
121 | _writer.WriteBoolean (_default.Value);
|
---|
122 | } else {
|
---|
123 | _writer.WriteNull ();
|
---|
124 | }
|
---|
125 |
|
---|
126 | _writer.WriteEndObject ();
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | private void writeString (ref JsonWriter _writer, ref bool _first, string _key, string _value) {
|
---|
131 | writeKeyType (ref _writer, ref _first, _key, "string");
|
---|
132 | _writer.WriteRaw (keyValue);
|
---|
133 | _writer.WriteString (_value);
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected void addItem (ref JsonWriter _writer, ref bool _first, string _key, string _value) {
|
---|
137 | writeString (ref _writer, ref _first, _key, _value);
|
---|
138 | _writer.WriteEndObject ();
|
---|
139 | }
|
---|
140 |
|
---|
141 | protected void addItem (ref JsonWriter _writer, ref bool _first, string _key, string _value, string _default) {
|
---|
142 | writeString (ref _writer, ref _first, _key, _value);
|
---|
143 |
|
---|
144 | _writer.WriteRaw (keyDefault);
|
---|
145 | _writer.WriteString (_default);
|
---|
146 |
|
---|
147 | _writer.WriteEndObject ();
|
---|
148 | }
|
---|
149 |
|
---|
150 | protected abstract void iterateList (ref JsonWriter _writer, ref bool _first);
|
---|
151 |
|
---|
152 | }
|
---|
153 | }
|
---|