Changeset 402 for binary-improvements2/MarkersMod/src/Markers.cs
- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MarkersMod/src/Markers.cs
r391 r402 1 1 using System.Collections.Generic; 2 2 using System.Net; 3 using AllocsFixes.JSON; 3 using JetBrains.Annotations; 4 using Utf8Json; 4 5 using Webserver; 5 6 using Webserver.WebAPI; 6 7 7 8 namespace Examples { 9 [UsedImplicitly] 8 10 public class Markers : AbsRestApi { 9 11 private const int numRandomMarkers = 5; … … 11 13 private readonly Dictionary<string, (int, int)> markers = new Dictionary<string, (int, int)> (); 12 14 13 private static readonly JsonArray emptyResult = new JsonArray ();14 15 15 public Markers () { 16 16 GameRandom random = GameRandomManager.Instance.CreateGameRandom (); … … 24 24 } 25 25 26 private static readonly byte[] jsonKeyId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("id"); 27 private static readonly byte[] jsonKeyLat = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lat"); 28 private static readonly byte[] jsonKeyLng = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lng"); 29 26 30 protected override void HandleRestGet (RequestContext _context) { 27 31 string id = _context.RequestPath; 28 32 33 PrepareEnvelopedResult (out JsonWriter writer); 34 29 35 if (string.IsNullOrEmpty (id)) { 30 JsonArray result = new JsonArray ();36 writer.WriteBeginArray (); 31 37 38 bool first = true; 32 39 foreach ((string markerId, (int, int) coordinates) in markers) { 33 JsonObject marker = new JsonObject (); 34 marker.Add ("id", new JsonString (markerId)); 35 marker.Add ("lat", new JsonNumber (coordinates.Item1)); 36 marker.Add ("lng", new JsonNumber (coordinates.Item2)); 37 result.Add (marker); 40 if (!first) { 41 writer.WriteValueSeparator (); 42 } 43 44 first = false; 45 46 writeMarkerJson (ref writer, markerId, coordinates); 38 47 } 39 40 SendEnvelopedResult (_context, result); 48 49 writer.WriteEndArray (); 50 SendEnvelopedResult (_context, ref writer); 41 51 return; 42 52 } 43 53 44 54 if (!markers.TryGetValue (id, out (int, int) location)) { 45 SendEnvelopedResult (_context, emptyResult, HttpStatusCode.NotFound); 55 writer.WriteRaw (JsonEmptyData); 56 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound); 46 57 return; 47 58 } 48 59 49 60 { 50 JsonArray result = new JsonArray (); 51 JsonObject marker = new JsonObject (); 52 marker.Add ("id", new JsonString (id)); 53 marker.Add ("lat", new JsonNumber (location.Item1)); 54 marker.Add ("lng", new JsonNumber (location.Item2)); 55 result.Add (marker); 56 SendEnvelopedResult (_context, result); 61 writer.WriteBeginArray (); 62 63 writeMarkerJson (ref writer, id, location); 64 65 writer.WriteEndArray (); 66 SendEnvelopedResult (_context, ref writer); 57 67 } 58 68 } 59 69 60 protected override void HandleRestPost (RequestContext _context, JsonNode _jsonBody) { 61 if (!(_jsonBody is JsonObject bodyObject)) { 62 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "BODY_NOT_OBJECT"); 70 private void writeMarkerJson (ref JsonWriter _writer, string _markerId, (int, int) _coordinates) { 71 _writer.WriteRaw (jsonKeyId); 72 _writer.WriteString (_markerId); 73 _writer.WriteRaw (jsonKeyLat); 74 _writer.WriteInt32 (_coordinates.Item1); 75 _writer.WriteRaw (jsonKeyLng); 76 _writer.WriteInt32 (_coordinates.Item2); 77 _writer.WriteEndObject (); 78 } 79 80 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { 81 if (!TryGetJsonField (_jsonInput, "lat", out int lat)) { 82 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT"); 63 83 return; 64 84 } 65 85 66 if (!TryGetJsonField (bodyObject, "lat", out int lat)) { 67 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LAT"); 68 return; 69 } 70 71 if (!TryGetJsonField (bodyObject, "lng", out int lng)) { 72 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LNG"); 86 if (!TryGetJsonField (_jsonInput, "lng", out int lng)) { 87 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG"); 73 88 return; 74 89 } … … 77 92 markers.Add (newId, (lat, lng)); 78 93 79 JsonString result = new JsonString (newId); 80 SendEnvelopedResult (_context, result, HttpStatusCode.Created); 94 PrepareEnvelopedResult (out JsonWriter writer); 95 writer.WriteString (newId); 96 SendEnvelopedResult (_context, ref writer, HttpStatusCode.Created); 81 97 } 82 98 83 protected override void HandleRestPut (RequestContext _context, JsonNode _jsonBody) {84 if (! (_jsonBody is JsonObject bodyObject)) {85 SendE nvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "BODY_NOT_OBJECT");99 protected override void HandleRestPut (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { 100 if (!TryGetJsonField (_jsonInput, "lat", out int lat)) { 101 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LAT"); 86 102 return; 87 103 } 88 104 89 if (!TryGetJsonField (bodyObject, "lat", out int lat)) { 90 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LAT"); 91 return; 92 } 93 94 if (!TryGetJsonField (bodyObject, "lng", out int lng)) { 95 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LNG"); 105 if (!TryGetJsonField (_jsonInput, "lng", out int lng)) { 106 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_LNG"); 96 107 return; 97 108 } … … 100 111 101 112 if (!markers.TryGetValue (id, out _)) { 102 SendE nvelopedResult (_context, null, HttpStatusCode.NotFound, _jsonBody, "ID_NOT_FOUND");113 SendErrorResult (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND"); 103 114 return; 104 115 } … … 106 117 markers [id] = (lat, lng); 107 118 108 JsonObject result = new JsonObject (); 109 result.Add ("id", new JsonString (id)); 110 result.Add ("lat", new JsonNumber (lat)); 111 result.Add ("lng", new JsonNumber (lng)); 112 SendEnvelopedResult (_context, result); 119 PrepareEnvelopedResult (out JsonWriter writer); 120 writer.WriteRaw (jsonKeyId); 121 writer.WriteString (id); 122 writer.WriteRaw (jsonKeyLat); 123 writer.WriteInt32 (lat); 124 writer.WriteRaw (jsonKeyLng); 125 writer.WriteInt32 (lng); 126 writer.WriteEndObject (); 127 SendEnvelopedResult (_context, ref writer); 113 128 } 114 129 … … 116 131 string id = _context.RequestPath; 117 132 118 SendEnvelopedResult (_context, null, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound); 133 PrepareEnvelopedResult (out JsonWriter writer); 134 writer.WriteRaw (JsonEmptyData); 135 SendEnvelopedResult (_context, ref writer, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound); 119 136 } 120 137 }
Note:
See TracChangeset
for help on using the changeset viewer.