Changeset 391 for binary-improvements2/MarkersMod/src/Markers.cs
- Timestamp:
- Aug 7, 2022, 3:02:24 PM (2 years ago)
- Location:
- binary-improvements2/MarkersMod/src
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MarkersMod/src/Markers.cs
r390 r391 2 2 using System.Net; 3 3 using AllocsFixes.JSON; 4 using AllocsFixes.NetConnections.Servers.Web;5 using AllocsFixes.NetConnections.Servers.Web.API;4 using Webserver; 5 using Webserver.WebAPI; 6 6 7 7 namespace Examples { 8 class Markers : AbsRestApi {9 private const int NumRandomMarkers = 5;8 public class Markers : AbsRestApi { 9 private const int numRandomMarkers = 5; 10 10 11 11 private readonly Dictionary<string, (int, int)> markers = new Dictionary<string, (int, int)> (); 12 12 13 private static readonly J SONArray emptyResult = new JSONArray ();13 private static readonly JsonArray emptyResult = new JsonArray (); 14 14 15 15 public Markers () { 16 16 GameRandom random = GameRandomManager.Instance.CreateGameRandom (); 17 17 18 for (int i = 0; i < NumRandomMarkers; i++) {18 for (int i = 0; i < numRandomMarkers; i++) { 19 19 int lat = random.RandomRange (-1000, 1001); 20 20 int lng = random.RandomRange (-1000, 1001); … … 28 28 29 29 if (string.IsNullOrEmpty (id)) { 30 J SONArray result = new JSONArray ();30 JsonArray result = new JsonArray (); 31 31 32 foreach ( KeyValuePair<string, (int, int)> kvpin markers) {33 J SONObject marker = new JSONObject ();34 marker.Add ("id", new J SONString (kvp.Key));35 marker.Add ("lat", new J SONNumber (kvp.Value.Item1));36 marker.Add ("lng", new J SONNumber (kvp.Value.Item2));32 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 37 result.Add (marker); 38 38 } … … 48 48 49 49 { 50 J SONArray result = new JSONArray ();51 J SONObject marker = new JSONObject ();52 marker.Add ("id", new J SONString (id));53 marker.Add ("lat", new J SONNumber (location.Item1));54 marker.Add ("lng", new J SONNumber (location.Item2));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 55 result.Add (marker); 56 56 SendEnvelopedResult (_context, result); … … 58 58 } 59 59 60 protected override void HandleRestPost (RequestContext _context, J SONNode _jsonBody) {61 if (!(_jsonBody is J SONObject bodyObject)) {60 protected override void HandleRestPost (RequestContext _context, JsonNode _jsonBody) { 61 if (!(_jsonBody is JsonObject bodyObject)) { 62 62 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "BODY_NOT_OBJECT"); 63 63 return; … … 77 77 markers.Add (newId, (lat, lng)); 78 78 79 J SONString result = new JSONString (newId);80 SendEnvelopedResult (_context, result );79 JsonString result = new JsonString (newId); 80 SendEnvelopedResult (_context, result, HttpStatusCode.Created); 81 81 } 82 82 83 protected override void HandleRestPut (RequestContext _context, J SONNode _jsonBody) {84 if (!(_jsonBody is J SONObject bodyObject)) {83 protected override void HandleRestPut (RequestContext _context, JsonNode _jsonBody) { 84 if (!(_jsonBody is JsonObject bodyObject)) { 85 85 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "BODY_NOT_OBJECT"); 86 86 return; … … 99 99 string id = _context.RequestPath; 100 100 101 if (!markers.TryGetValue (id, out (int, int) location)) {101 if (!markers.TryGetValue (id, out _)) { 102 102 SendEnvelopedResult (_context, null, HttpStatusCode.NotFound, _jsonBody, "ID_NOT_FOUND"); 103 103 return; … … 106 106 markers [id] = (lat, lng); 107 107 108 J SONObject result = new JSONObject ();109 result.Add ("id", new J SONString (id));110 result.Add ("lat", new J SONNumber (lat));111 result.Add ("lng", new J SONNumber (lng));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 112 SendEnvelopedResult (_context, result); 113 113 }
Note:
See TracChangeset
for help on using the changeset viewer.