Kinetica   C#   API  Version 7.2.3.0
JsonHelper.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
20 using System.Text;
21 using Newtonsoft.Json.Linq;
22 using Newtonsoft.Json;
23 
24 namespace Avro
25 {
26  class JsonHelper
27  {
35  public static string GetOptionalString(JToken jtok, string field)
36  {
37  if (null == jtok) throw new ArgumentNullException("jtok", "jtok cannot be null.");
38  if (string.IsNullOrEmpty(field)) throw new ArgumentNullException("field", "field cannot be null.");
39 
40  JToken child = jtok[field];
41  if (null == child) return null;
42 
43  if (child.Type == JTokenType.String)
44  {
45  string value = child.ToString();
46  return value.Trim('\"');
47  }
48  throw new SchemaParseException("Field " + field + " is not a string");
49  }
50 
57  public static string GetRequiredString(JToken jtok, string field)
58  {
59  string value = GetOptionalString(jtok, field);
60  if (string.IsNullOrEmpty(value)) throw new SchemaParseException(string.Format("No \"{0}\" JSON field: {1}", field, jtok));
61  return value;
62  }
63 
70  public static int GetRequiredInteger(JToken jtok, string field)
71  {
72  ensureValidFieldName(field);
73  JToken child = jtok[field];
74  if (null == child) throw new SchemaParseException(string.Format("No \"{0}\" JSON field: {1}", field, jtok));
75 
76  if (child.Type == JTokenType.Integer) return (int) child;
77  throw new SchemaParseException("Field " + field + " is not an integer");
78  }
79 
86  public static bool? GetOptionalBoolean(JToken jtok, string field)
87  {
88  if (null == jtok) throw new ArgumentNullException("jtok", "jtok cannot be null.");
89  if (string.IsNullOrEmpty(field)) throw new ArgumentNullException("field", "field cannot be null.");
90 
91  JToken child = jtok[field];
92  if (null == child) return null;
93 
94  if (child.Type == JTokenType.Boolean)
95  return (bool)child;
96 
97  throw new SchemaParseException("Field " + field + " is not a boolean");
98  }
99 
106  internal static void writeIfNotNullOrEmpty(JsonTextWriter writer, string key, string value)
107  {
108  if (string.IsNullOrEmpty(value)) return;
109  writer.WritePropertyName(key);
110  writer.WriteValue(value);
111  }
112 
117  private static void ensureValidFieldName(string name)
118  {
119  if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
120  }
121  }
122 }
static ? bool GetOptionalBoolean(JToken jtok, string field)
Retrieves the optional boolean property value for the given property name from the given JSON object.
Definition: JsonHelper.cs:86
static string GetRequiredString(JToken jtok, string field)
Retrieves the required string property value for the given property name from the given JSON object.
Definition: JsonHelper.cs:57
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
static string GetOptionalString(JToken jtok, string field)
Retrieves the optional string property value for the given property name from the given JSON object.
Definition: JsonHelper.cs:35
static int GetRequiredInteger(JToken jtok, string field)
Retrieves the required int property value for the given property name from the given JSON object.
Definition: JsonHelper.cs:70