private string EncodeValue(string v) { if (v == string.Empty) { return("\"\""); } var fchar = v[0]; if (char.IsLetter(fchar) || (char.Equals('0', fchar) && v.Length > 1)) // probably a string. don't parse { goto return_string; } try { JToken token = JToken.Parse(v); return(v); } catch { goto return_string; } return_string: return(JsonStringProcessor.escape(v)); }
public override string ToString() { if (s_builder == null) { s_builder = new StringBuilder(); } else { s_builder.Clear(); } s_builder.Append('{'); s_builder.Append("\"CellID\":"); s_builder.Append(id); for (int i = 0, len = keys.Count; i < len; ++i) { s_builder.Append(','); s_builder.Append(JsonStringProcessor.escape(keys[i])); s_builder.Append(':'); s_builder.Append(JsonStringProcessor.escape(values[i])); } s_builder.Append('}'); return(s_builder.ToString()); }
private static void ToString_impl(string value, StringBuilder str_builder, bool in_json) { if (in_json) { str_builder.Append(JsonStringProcessor.escape(value)); } else { str_builder.Append(value); } }
private void WriteResponseForBadRequest(BadRequestException ex, HttpListenerContext ctx) { ctx.Response.StatusCode = (int)HttpStatusCode.BadRequest; using (var writer = new StreamWriter(ctx.Response.OutputStream)) { writer.Write(@"{ ""error"": { ""code"": " + JsonStringProcessor.escape(ex.Code) + @", ""message"": " + JsonStringProcessor.escape(ex.Message) + @" } }"); } }
private void BuildCellGroupContent(ref bool empty, ICell sub_cell, StringBuilder sb, bool include_cell_id) { if (sub_cell != null) { List <string> available_field_names = sub_cell.GetFieldNames().ToList(); foreach (var field_desc in sub_cell.GetFieldDescriptors()) { if (field_desc.Attributes.ContainsKey("GraphEdge")) { continue; } if (!available_field_names.Contains(field_desc.Name)) { continue; } string field_value = sub_cell.GetField <string>(field_desc.Name); if (field_desc.Type == typeof(string)) { field_value = JsonStringProcessor.escape(field_value); } if (empty) { empty = false; } else { sb.Append(','); } sb.Append(String.Format(@"""{0}"":{1}", field_desc.Name, field_value)); } if (include_cell_id) { if (empty) { empty = false; } else { sb.Append(','); } sb.Append(String.Format("\"CellId\":{0}", sub_cell.CellId)); } } }
public void Serialize(TextWriter writer) { writer.Write('{'); writer.Write("\"CellID\":"); writer.Write(id); for (int i = 0, len = keys.Count; i < len; ++i) { writer.Write(','); writer.Write(JsonStringProcessor.escape(keys[i])); writer.Write(':'); writer.Write(EncodeValue(values[i])); } writer.Write('}'); }
public override void GetNodesInfoHandler(GetNodesInfoRequest request, System.Net.HttpListenerResponse response) { var writers = Enumerable.Range(0, Global.CloudStorage.PartitionCount).Select(i => new GetNodesInfoRequestWriter(fields: new List <string> { "type_object_name" })).ToArray(); foreach (var id in request.ids) { writers[Global.CloudStorage.GetPartitionIdByCellId(id)].ids.Add(id); } var readers = writers.Select((writer, server) => _GetNodesInfo_impl(server, writer)).ToArray(); var results = readers.Aggregate( (IEnumerable <NodeInfo>) new List <NodeInfo>(), (_, reader) => Enumerable.Concat(_, reader.infoList.Select(infoAccessor => (NodeInfo)infoAccessor))); string result_string = "[" + string.Join(",", results.Select(_ => string.Format(CultureInfo.InvariantCulture, @"{{""CellID"": {0}, ""type_object_name"": {1}}}", _.id, JsonStringProcessor.escape(_.values.First())))) + "]"; using (var sw = new StreamWriter(response.OutputStream)) { sw.Write(result_string); } }
private static void ToString_impl(t_data_type value, StringBuilder str_builder, bool in_json) { //For top-level element, just return the literal representation. //For non-top-level element, it would be an element in a json structure. (object, or array) //Thus we have to escape strings, surround fields with quotes (") if necessary. IF("$t_data_type->is_string()"); if (in_json) { str_builder.Append(JsonStringProcessor.escape(value)); } else { str_builder.Append(value); } ELIF("$t_data_type->is_struct()"); { IF("$t_data_type->is_optional()"); if (value == null) { return; } END(); str_builder.Append('{'); bool first_field = true; FOREACH(); { IF("$t_field_type->is_nullable() || !$t_field_type->is_value_type()"); IF("$t_data_type->is_optional()"); if (value.Value.t_field_name != null) { ELSE(); } if (value.t_field_name != null) /*END*/ { END(); if (first_field) { first_field = false; } else { str_builder.Append(','); } str_builder.Append("\"t_field_name\":"); IF("$t_data_type->is_optional()"); ToString_impl(value.Value.t_field_name, str_builder, in_json: true); ELSE(); ToString_impl(value.t_field_name, str_builder, in_json: true); END(); IF("$t_field_type->is_nullable() || !$t_field_type->is_value_type()"); } END(); } END(); str_builder.Append('}'); } ELIF("$t_data_type->is_value_type()"); { IF("$t_data_type->is_enum() || $t_data_type->is_datetime() || $t_data_type->is_guid()"); if (in_json) { str_builder.Append('"'); } END(); IF("$t_data_type->is_bool()"); { str_builder.Append(value.ToString().ToLowerInvariant()); } ELIF("$t_data_type->is_datetime()"); { IF("$t_data_type->is_nullable()"); { str_builder.Append(value.Value.ToString("o", CultureInfo.InvariantCulture)); } ELSE(); { str_builder.Append(value.ToString("o", CultureInfo.InvariantCulture)); } END(); } ELSE(); { str_builder.Append(value); } END(); IF("$t_data_type->is_enum() || $t_data_type->is_datetime() || $t_data_type->is_guid()"); if (in_json) { str_builder.Append('"'); } END(); } ELIF("$t_data_type->is_array() || $t_data_type->is_list()"); { str_builder.Append('['); bool first = true; foreach (var element in value) { if (first) { first = false; } else { str_builder.Append(','); } ToString_impl(element, str_builder, in_json: true); } str_builder.Append(']'); } ELSE(); throw new Exception("Internal error T5007"); END(); }
internal static void ToString(string value, StringBuilder str_builder) { str_builder.Append(JsonStringProcessor.escape(value)); }