[425] | 1 | using System;
|
---|
[434] | 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Globalization;
|
---|
[425] | 4 | using UnityEngine;
|
---|
[402] | 5 | using Utf8Json;
|
---|
| 6 |
|
---|
| 7 | namespace 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 |
|
---|
[408] | 13 | public static void WritePositionObject (ref JsonWriter _writer, Vector3i _position) {
|
---|
[402] | 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 | }
|
---|
[425] | 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 |
|
---|
[434] | 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 |
|
---|
[425] | 83 | public static void WriteDateTime (ref JsonWriter _writer, DateTime _dateTime) {
|
---|
| 84 | _writer.WriteString (_dateTime.ToString ("o"));
|
---|
| 85 | }
|
---|
[426] | 86 |
|
---|
[434] | 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;
|
---|
[426] | 92 | }
|
---|
[434] | 93 |
|
---|
| 94 | return DateTime.TryParse (dateTimeString, null, DateTimeStyles.RoundtripKind, out _result);
|
---|
[426] | 95 | }
|
---|
[434] | 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 | }
|
---|
[402] | 174 | }
|
---|
| 175 | }
|
---|