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

Last change on this file since 475 was 455, checked in by alloc, 16 months ago

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 2.2 KB
RevLine 
[279]1using System;
[455]2using JetBrains.Annotations;
3using Utf8Json;
[454]4using Webserver;
5using Webserver.WebAPI;
[279]6
[454]7namespace AllocsFixes.WebAPIs {
[455]8 [UsedImplicitly]
[454]9 public class GetServerInfo : AbsWebAPI {
[455]10 private static readonly byte[] jsonKeyType = JsonWriter.GetEncodedPropertyNameWithBeginObject ("type");
11 private static readonly byte[] jsonKeyValue = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("value");
12
[454]13 public override void HandleRequest (RequestContext _context) {
[360]14 GameServerInfo gsi = ConnectionManager.Instance.LocalServerInfo;
[279]15
[455]16 JsonWriter writer = new JsonWriter ();
17 writer.WriteBeginObject ();
18
19 bool first = true;
20
[325]21 foreach (string stringGamePref in Enum.GetNames (typeof (GameInfoString))) {
22 string value = gsi.GetValue ((GameInfoString) Enum.Parse (typeof (GameInfoString), stringGamePref));
[455]23
24 if (!first) {
25 writer.WriteValueSeparator ();
26 }
[279]27
[455]28 first = false;
[279]29
[455]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 ();
[279]38 }
39
[325]40 foreach (string intGamePref in Enum.GetNames (typeof (GameInfoInt))) {
41 int value = gsi.GetValue ((GameInfoInt) Enum.Parse (typeof (GameInfoInt), intGamePref));
[279]42
[455]43 if (!first) {
44 writer.WriteValueSeparator ();
45 }
[279]46
[455]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 ();
[279]57 }
58
[325]59 foreach (string boolGamePref in Enum.GetNames (typeof (GameInfoBool))) {
60 bool value = gsi.GetValue ((GameInfoBool) Enum.Parse (typeof (GameInfoBool), boolGamePref));
[279]61
[455]62 if (!first) {
63 writer.WriteValueSeparator ();
64 }
[279]65
[455]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 ();
[279]76 }
[455]77
78 writer.WriteEndObject ();
79
80 WebUtils.WriteJsonData (_context.Response, ref writer);
[279]81 }
82 }
[325]83}
Note: See TracBrowser for help on using the repository browser.