source: binary-improvements2/7dtd-server-fixes/src/JSON/JSONBoolean.cs@ 391

Last change on this file since 391 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 1.0 KB
Line 
1using System.Text;
2
3namespace AllocsFixes.JSON {
4 public class JsonBoolean : JsonValue {
5 private readonly bool value;
6
7 public JsonBoolean (bool _value) {
8 value = _value;
9 }
10
11 public bool GetBool () {
12 return value;
13 }
14
15 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) {
16 _stringBuilder.Append (value ? "true" : "false");
17 }
18
19 public static JsonBoolean Parse (string _json, ref int _offset) {
20 //Log.Out ("ParseBool enter (" + offset + ")");
21
22 if (_json.Substring (_offset, 4).Equals ("true")) {
23 //Log.Out ("JSON:Parsed Bool: true");
24 _offset += 4;
25 return new JsonBoolean (true);
26 }
27
28 if (_json.Substring (_offset, 5).Equals ("false")) {
29 //Log.Out ("JSON:Parsed Bool: false");
30 _offset += 5;
31 return new JsonBoolean (false);
32 }
33
34 throw new MalformedJsonException ("No valid boolean found");
35 }
36
37 public override string AsString => value ? "true" : "false";
38 public override int AsInt => value ? 1 : 0;
39 public override double AsDouble => AsInt;
40 }
41}
Note: See TracBrowser for help on using the repository browser.