source: binary-improvements/MapRendering/API/GetServerInfo.cs@ 501

Last change on this file since 501 was 455, checked in by alloc, 17 months ago

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 2.2 KB
Line 
1using System;
2using JetBrains.Annotations;
3using Utf8Json;
4using Webserver;
5using Webserver.WebAPI;
6
7namespace AllocsFixes.WebAPIs {
8 [UsedImplicitly]
9 public class GetServerInfo : AbsWebAPI {
10 private static readonly byte[] jsonKeyType = JsonWriter.GetEncodedPropertyNameWithBeginObject ("type");
11 private static readonly byte[] jsonKeyValue = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("value");
12
13 public override void HandleRequest (RequestContext _context) {
14 GameServerInfo gsi = ConnectionManager.Instance.LocalServerInfo;
15
16 JsonWriter writer = new JsonWriter ();
17 writer.WriteBeginObject ();
18
19 bool first = true;
20
21 foreach (string stringGamePref in Enum.GetNames (typeof (GameInfoString))) {
22 string value = gsi.GetValue ((GameInfoString) Enum.Parse (typeof (GameInfoString), stringGamePref));
23
24 if (!first) {
25 writer.WriteValueSeparator ();
26 }
27
28 first = false;
29
30 writer.WritePropertyName (stringGamePref);
31 writer.WriteRaw (jsonKeyType);
32 writer.WriteString ("string");
33
34 writer.WriteRaw (jsonKeyValue);
35 writer.WriteString (value);
36
37 writer.WriteEndObject ();
38 }
39
40 foreach (string intGamePref in Enum.GetNames (typeof (GameInfoInt))) {
41 int value = gsi.GetValue ((GameInfoInt) Enum.Parse (typeof (GameInfoInt), intGamePref));
42
43 if (!first) {
44 writer.WriteValueSeparator ();
45 }
46
47 first = false;
48
49 writer.WritePropertyName (intGamePref);
50 writer.WriteRaw (jsonKeyType);
51 writer.WriteString ("int");
52
53 writer.WriteRaw (jsonKeyValue);
54 writer.WriteInt32 (value);
55
56 writer.WriteEndObject ();
57 }
58
59 foreach (string boolGamePref in Enum.GetNames (typeof (GameInfoBool))) {
60 bool value = gsi.GetValue ((GameInfoBool) Enum.Parse (typeof (GameInfoBool), boolGamePref));
61
62 if (!first) {
63 writer.WriteValueSeparator ();
64 }
65
66 first = false;
67
68 writer.WritePropertyName (boolGamePref);
69 writer.WriteRaw (jsonKeyType);
70 writer.WriteString ("bool");
71
72 writer.WriteRaw (jsonKeyValue);
73 writer.WriteBoolean (value);
74
75 writer.WriteEndObject ();
76 }
77
78 writer.WriteEndObject ();
79
80 WebUtils.WriteJsonData (_context.Response, ref writer);
81 }
82 }
83}
Note: See TracBrowser for help on using the repository browser.