source: binary-improvements2/WebServer/src/WebAPI/JsonCommons.cs@ 434

Last change on this file since 434 was 434, checked in by alloc, 18 months ago

Added permission management APIs

File size: 5.2 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 WritePositionObject (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 WritePositionObject (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 private static readonly byte[] jsonKeyCombinedString = JsonWriter.GetEncodedPropertyNameWithBeginObject ("combinedString");
40 private static readonly byte[] jsonKeyPlatformId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("platformId");
41 private static readonly byte[] jsonKeyUserId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("userId");
42
43 public static void WritePlatformUserIdentifier (ref JsonWriter _writer, PlatformUserIdentifierAbs _userIdentifier) {
44 if (_userIdentifier == null) {
45 _writer.WriteNull ();
46 return;
47 }
48
49 _writer.WriteRaw (jsonKeyCombinedString);
50 _writer.WriteString (_userIdentifier.CombinedString);
51
52 _writer.WriteRaw (jsonKeyPlatformId);
53 _writer.WriteString (_userIdentifier.PlatformIdentifierString);
54
55 _writer.WriteRaw (jsonKeyUserId);
56 _writer.WriteString (_userIdentifier.ReadablePlatformUserIdentifier);
57
58 _writer.WriteEndObject ();
59 }
60
61 public static bool TryReadPlatformUserIdentifier (IDictionary<string, object> _jsonInput, out PlatformUserIdentifierAbs _userIdentifier) {
62 if (TryGetJsonField (_jsonInput, "combinedString", out string combinedString)) {
63 _userIdentifier = PlatformUserIdentifierAbs.FromCombinedString (combinedString, false);
64 if (_userIdentifier != null) {
65 return true;
66 }
67 }
68
69 if (!TryGetJsonField (_jsonInput, "platformId", out string platformId)) {
70 _userIdentifier = default;
71 return false;
72 }
73
74 if (!TryGetJsonField (_jsonInput, "userId", out string userId)) {
75 _userIdentifier = default;
76 return false;
77 }
78
79 _userIdentifier = PlatformUserIdentifierAbs.FromPlatformAndId (platformId, userId, false);
80 return _userIdentifier != null;
81 }
82
83 public static void WriteDateTime (ref JsonWriter _writer, DateTime _dateTime) {
84 _writer.WriteString (_dateTime.ToString ("o"));
85 }
86
87 public static bool TryReadDateTime (IDictionary<string, object> _jsonInput, string _fieldName, out DateTime _result) {
88 _result = default;
89
90 if (!TryGetJsonField (_jsonInput, _fieldName, out string dateTimeString)) {
91 return false;
92 }
93
94 return DateTime.TryParse (dateTimeString, null, DateTimeStyles.RoundtripKind, out _result);
95 }
96
97
98 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out int _value) {
99 _value = default;
100
101 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
102 return false;
103 }
104
105 if (fieldNode is not double value) {
106 return false;
107 }
108
109 try {
110 _value = (int)value;
111 return true;
112 } catch (Exception) {
113 return false;
114 }
115 }
116
117 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out double _value) {
118 _value = default;
119
120 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
121 return false;
122 }
123
124 if (fieldNode is not double value) {
125 return false;
126 }
127
128 try {
129 _value = value;
130 return true;
131 } catch (Exception) {
132 return false;
133 }
134 }
135
136 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out string _value) {
137 _value = default;
138
139 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
140 return false;
141 }
142
143 if (fieldNode is not string value) {
144 return false;
145 }
146
147 try {
148 _value = value;
149 return true;
150 } catch (Exception) {
151 return false;
152 }
153 }
154
155 public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName,
156 out IDictionary<string, object> _value) {
157 _value = default;
158
159 if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
160 return false;
161 }
162
163 if (fieldNode is not IDictionary<string, object> value) {
164 return false;
165 }
166
167 try {
168 _value = value;
169 return true;
170 } catch (Exception) {
171 return false;
172 }
173 }
174 }
175}
Note: See TracBrowser for help on using the repository browser.