Changeset 434 for binary-improvements2/WebServer/src/WebAPI/JsonCommons.cs
- Timestamp:
- May 17, 2023, 11:05:59 PM (18 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/WebAPI/JsonCommons.cs
r426 r434 1 1 using System; 2 using System.Collections.Generic; 3 using System.Globalization; 2 4 using UnityEngine; 3 5 using Utf8Json; … … 57 59 } 58 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 59 83 public static void WriteDateTime (ref JsonWriter _writer, DateTime _dateTime) { 60 84 _writer.WriteString (_dateTime.ToString ("o")); 61 85 } 62 86 63 public static void WriteStringOrNull (ref JsonWriter _writer, string _string) { 64 if (_string == null) { 65 _writer.WriteNull (); 66 } else { 67 _writer.WriteString (_string); 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; 68 172 } 69 173 }
Note:
See TracChangeset
for help on using the changeset viewer.