CJsonNode g_GenericStatToJson(CNetServer server, ENetScheduleStatTopic topic, bool verbose) { string stat_cmd(s_StatTopics[topic].command); CTempString prefix(s_StatTopics[topic].record_prefix); CTempString entity_name(s_StatTopics[topic].entity_name); if (verbose) stat_cmd.append(" VERBOSE"); CNetServerMultilineCmdOutput output(server.ExecWithRetry(stat_cmd, true)); CJsonNode entities(CJsonNode::NewArrayNode()); CJsonNode entity_info; CJsonNode array_value; string line; while (output.ReadLine(line)) { if (NStr::StartsWith(line, prefix)) { if (entity_info) entities.Append(entity_info); entity_info = CJsonNode::NewObjectNode(); entity_info.SetString(entity_name, UnquoteIfQuoted( CTempString(line.data() + prefix.length(), line.length() - prefix.length()))); } else if (entity_info && NStr::StartsWith(line, " ")) { if (NStr::StartsWith(line, " ") && array_value) { array_value.AppendString(UnquoteIfQuoted( NStr::TruncateSpaces(line, NStr::eTrunc_Begin))); } else { if (array_value) array_value = NULL; CTempString key, value; NStr::SplitInTwo(line, ":", key, value); NormalizeStatKeyName(key); string key_norm(key); value = NStr::TruncateSpaces_Unsafe(value, NStr::eTrunc_Begin); if (value.empty()) entity_info.SetByKey(key_norm, array_value = CJsonNode::NewArrayNode()); else entity_info.SetByKey(key_norm, CJsonNode::GuessType(value)); } } } if (entity_info) entities.Append(entity_info); return entities; }
void g_DetectTypeAndSet(CJsonNode& node, const CTempString& key, const CTempString& value) { if (IsInteger(value)) node.SetNumber(key, NStr::StringToInt8(value)); else if (NStr::CompareNocase(value, "FALSE") == 0) node.SetBoolean(key, false); else if (NStr::CompareNocase(value, "TRUE") == 0) node.SetBoolean(key, true); else if (NStr::CompareNocase(value, "NONE") == 0) node.SetNull(key); else node.SetString(key, UnquoteIfQuoted(value)); }