source: TFP-WebServer/WebServer/src/WebAPI/JsonCommons.cs@ 449

Last change on this file since 449 was 444, checked in by alloc, 17 months ago

Added more details to API map/config
Updated web files to 0.7.0

File size: 6.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using UnityEngine;
5using Utf8Json;
6
7namespace Webserver.WebAPI {
8 public static class JsonCommons {
9 private static readonly byte[] jsonKeyPositionX = JsonWriter.GetEncodedPropertyNameWithBeginObject ("x");
10 private static readonly byte[] jsonKeyPositionY = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("y");
11 private static readonly byte[] jsonKeyPositionZ = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("z");
12
13 public static void WriteVector3I (ref JsonWriter _writer, Vector3i _position) {
14 _writer.WriteRaw (jsonKeyPositionX);
15 _writer.WriteInt32 (_position.x);
16
17 _writer.WriteRaw (jsonKeyPositionY);
18 _writer.WriteInt32 (_position.y);
19
20 _writer.WriteRaw (jsonKeyPositionZ);
21 _writer.WriteInt32 (_position.z);
22
23 _writer.WriteEndObject ();
24 }
25
26 public static void WriteVector3 (ref JsonWriter _writer, Vector3 _position) {
27 _writer.WriteRaw (jsonKeyPositionX);
28 _writer.WriteSingle (_position.x);
29
30 _writer.WriteRaw (jsonKeyPositionY);
31 _writer.WriteSingle (_position.y);
32
33 _writer.WriteRaw (jsonKeyPositionZ);
34 _writer.WriteSingle (_position.z);
35
36 _writer.WriteEndObject ();
37 }
38
39 public static void WriteVector2I (ref JsonWriter _writer, Vector2i _position) {
40 _writer.WriteRaw (jsonKeyPositionX);
41 _writer.WriteInt32 (_position.x);
42
43 _writer.WriteRaw (jsonKeyPositionY);
44 _writer.WriteInt32 (_position.y);
45
46 _writer.WriteEndObject ();
47 }
48
49 public static void WriteVector2 (ref JsonWriter _writer, Vector2 _position) {
50 _writer.WriteRaw (jsonKeyPositionX);
51 _writer.WriteSingle (_position.x);
52
53 _writer.WriteRaw (jsonKeyPositionY);
54 _writer.WriteSingle (_position.y);
55
56 _writer.WriteEndObject ();
57 }
58
59 private static readonly byte[] jsonKeyDays = JsonWriter.GetEncodedPropertyNameWithBeginObject ("days");
60 private static readonly byte[] jsonKeyHours = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("hours");
61 private static readonly byte[] jsonKeyMinutes = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("minutes");
62
63 public static void WriteGameTimeObject (ref JsonWriter _writer, int _days, int _hours, int _minutes) {
64 _writer.WriteRaw (jsonKeyDays);
65 _writer.WriteInt32 (_days);
66
67 _writer.WriteRaw (jsonKeyHours);
68 _writer.WriteInt32 (_hours);
69
70 _writer.WriteRaw (jsonKeyMinutes);
71 _writer.WriteInt32 (_minutes);
72
73 _writer.WriteEndObject ();
74 }
75
76 private static readonly byte[] jsonKeyCombinedString = JsonWriter.GetEncodedPropertyNameWithBeginObject ("combinedString");
77 private static readonly byte[] jsonKeyPlatformId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("platformId");
78 private static readonly byte[] jsonKeyUserId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("userId");
79
80 public static void WritePlatformUserIdentifier (ref JsonWriter _writer, PlatformUserIdentifierAbs _userIdentifier) {
81 if (_userIdentifier == null) {
82 _writer.WriteNull ();
83 return;
84 }
85
86 _writer.WriteRaw (jsonKeyCombinedString);
87 _writer.WriteString (_userIdentifier.CombinedString);
88
89 _writer.WriteRaw (jsonKeyPlatformId);
90 _writer.WriteString (_userIdentifier.PlatformIdentifierString);
91
92 _writer.WriteRaw (jsonKeyUserId);
93 _writer.WriteString (_userIdentifier.ReadablePlatformUserIdentifier);
94
95 _writer.WriteEndObject ();
96 }
97
98 public static bool TryReadPlatformUserIdentifier (IDictionary<string, object> _jsonInput, out PlatformUserIdentifierAbs _userIdentifier) {
99 if (TryGetJsonField (_jsonInput, "combinedString", out string combinedString)) {
100 _userIdentifier = PlatformUserIdentifierAbs.FromCombinedString (combinedString, false);
101 if (_userIdentifier != null) {
102 return true;
103 }
104 }
105
106 if (!TryGetJsonField (_jsonInput, "platformId", out string platformId)) {
107 _userIdentifier = default;
108 return false;
109 }
110
111 if (!TryGetJsonField (_jsonInput, "userId", out string userId)) {
112 _userIdentifier = default;
113 return false;
114 }
115
116 _userIdentifier = PlatformUserIdentifierAbs.FromPlatformAndId (platformId, userId, false);
117 return _userIdentifier != null;
118 }
119
120 public static void WriteDateTime (ref JsonWriter _writer, DateTime _dateTime) {
121 _writer.WriteString (_dateTime.ToString ("o"));
122 }
123
124 public static bool TryReadDateTime (IDictionary<string, object> _jsonInput, string _fieldName, out DateTime _result) {
125 _result = default;
126
127 if (!TryGetJsonField (_jsonInput, _fieldName, out string dateTimeString)) {
128 return false;
129 }
130
131 return DateTime.TryParse (dateTimeString, null, DateTimeStyles.RoundtripKind, out _result);
132 }
133
134
135 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out int _value) {
136 _value = default;
137
138 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
139 return false;
140 }
141
142 if (fieldNode is not double value) {
143 return false;
144 }
145
146 try {
147 _value = (int)value;
148 return true;
149 } catch (Exception) {
150 return false;
151 }
152 }
153
154 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out double _value) {
155 _value = default;
156
157 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
158 return false;
159 }
160
161 if (fieldNode is not double value) {
162 return false;
163 }
164
165 try {
166 _value = value;
167 return true;
168 } catch (Exception) {
169 return false;
170 }
171 }
172
173 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out string _value) {
174 _value = default;
175
176 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
177 return false;
178 }
179
180 if (fieldNode is not string value) {
181 return false;
182 }
183
184 try {
185 _value = value;
186 return true;
187 } catch (Exception) {
188 return false;
189 }
190 }
191
192 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName,
193 out IDictionary<string, object> _value) {
194 _value = default;
195
196 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
197 return false;
198 }
199
200 if (fieldNode is not IDictionary<string, object> value) {
201 return false;
202 }
203
204 try {
205 _value = value;
206 return true;
207 } catch (Exception) {
208 return false;
209 }
210 }
211 }
212}
Note: See TracBrowser for help on using the repository browser.