Changed json output to an object stream based format

This commit is contained in:
Jordan Cristiano 2015-07-05 23:42:08 -04:00
parent 43745b85f5
commit 2e597565bf
3 changed files with 65 additions and 10 deletions

View File

@ -25,6 +25,10 @@ namespace base
void JsonWriterFile::Flush() void JsonWriterFile::Flush()
{ {
m_fileStream.Flush(); m_fileStream.Flush();
void JsonWriterFile::Reset()
{
m_writer.Reset(m_fileStream);
} }
bool JsonWriterFile::IsComplete() const bool JsonWriterFile::IsComplete() const
@ -62,6 +66,13 @@ namespace base
m_writer.EndArray(); m_writer.EndArray();
} }
void JsonWriterFile::WriteNull(const char* name)
{
auto& writer = m_writer;
writer.String(name);
writer.Null();
}
void JsonWriterFile::WriteBool(const char* name, bool value) void JsonWriterFile::WriteBool(const char* name, bool value)
{ {
auto& writer = m_writer; auto& writer = m_writer;
@ -76,6 +87,20 @@ namespace base
writer.Int(value); writer.Int(value);
} }
void JsonWriterFile::WriteInt32(const char* name, std::int32_t value, bool writeCondition)
{
auto& writer = m_writer;
writer.String(name);
if (writeCondition)
{
writer.Int(value);
}
else
{
writer.Null();
}
}
void JsonWriterFile::WriteInt64(const char* name, std::int64_t value) void JsonWriterFile::WriteInt64(const char* name, std::int64_t value)
{ {
auto& writer = m_writer; auto& writer = m_writer;
@ -90,6 +115,20 @@ namespace base
writer.Uint(value); writer.Uint(value);
} }
void JsonWriterFile::WriteUInt32(const char* name, std::uint32_t value, bool writeCondition)
{
auto& writer = m_writer;
writer.String(name);
if (writeCondition)
{
writer.Uint(value);
}
else
{
writer.Null();
}
}
void JsonWriterFile::WriteUint64(const char* name, std::uint64_t value) void JsonWriterFile::WriteUint64(const char* name, std::uint64_t value)
{ {
auto& writer = m_writer; auto& writer = m_writer;

View File

@ -17,6 +17,7 @@ namespace base
FILE* GetFp() const; FILE* GetFp() const;
void Flush(); void Flush();
void Reset();
bool IsComplete() const; bool IsComplete() const;
void StartObject(const char* name = nullptr); void StartObject(const char* name = nullptr);
@ -28,8 +29,10 @@ namespace base
void WriteNull(const char* name); void WriteNull(const char* name);
void WriteBool(const char* name, bool value); void WriteBool(const char* name, bool value);
void WriteInt32(const char* name, std::int32_t value); void WriteInt32(const char* name, std::int32_t value);
void WriteInt32(const char* name, std::int32_t value, bool writeCondition);
void WriteInt64(const char* name, std::int64_t value); void WriteInt64(const char* name, std::int64_t value);
void WriteUInt32(const char* name, std::uint32_t value); void WriteUInt32(const char* name, std::uint32_t value);
void WriteUInt32(const char* name, std::uint32_t value, bool writeCondition);
void WriteUint64(const char* name, std::uint64_t value); void WriteUint64(const char* name, std::uint64_t value);
void WriteString(const char* name, const char* value); void WriteString(const char* name, const char* value);
void WriteString(const char* name, const char* value, std::int32_t length); void WriteString(const char* name, const char* value, std::int32_t length);

View File

@ -41,50 +41,63 @@ JsonWriter::JsonWriter(FILE* outputFp):
void JsonWriter::StartWriting(demoheader_t& header) void JsonWriter::StartWriting(demoheader_t& header)
{ {
auto& jsonFile = m_jsonFile; auto& jsonFile = m_jsonFile;
jsonFile.Reset();
jsonFile.StartObject(); jsonFile.StartObject();
DemoJsonWriter::WriteDemoHeader(jsonFile, header); DemoJsonWriter::WriteDemoHeader(jsonFile, header);
jsonFile.StartArray("demmessages"); jsonFile.EndObject();
} }
void JsonWriter::EndWriting() void JsonWriter::EndWriting()
{ {
auto& jsonFile = m_jsonFile; auto& jsonFile = m_jsonFile;
jsonFile.EndArray(); jsonFile.Flush();
jsonFile.EndObject();
assert(jsonFile.IsComplete()); assert(jsonFile.IsComplete());
} }
void JsonWriter::StartCommandPacket(const CommandPacket& packet) void JsonWriter::StartCommandPacket(const CommandPacket& packet)
{ {
auto& jsonFile = m_jsonFile; auto& jsonFile = m_jsonFile;
jsonFile.Reset();
jsonFile.StartObject(); jsonFile.StartObject();
jsonFile.WriteInt32("tick", packet.tick); jsonFile.WriteInt32("tick", packet.tick);
jsonFile.StartObject(DemoCmdToString(packet.cmd)); jsonFile.WriteString("cmd", DemoCmdToString(packet.cmd));
jsonFile.EndObject();
jsonFile.Reset();
jsonFile.StartObject();
DemHandlers::DemMsg_JsonWrite(packet.cmd, jsonFile, packet.data); DemHandlers::DemMsg_JsonWrite(packet.cmd, jsonFile, packet.data);
jsonFile.EndObject();
assert(jsonFile.IsComplete());
if (packet.cmd == dem_packet || packet.cmd == dem_signon) if (packet.cmd == dem_packet || packet.cmd == dem_signon)
{ {
m_writingNetPackets = true; m_writingNetPackets = true;
jsonFile.StartArray("netpackets");
} }
} }
void JsonWriter::EndCommandPacket(const PacketTrailingBits& trailingBits) void JsonWriter::EndCommandPacket(const PacketTrailingBits& trailingBits)
{ {
auto& jsonFile = m_jsonFile;
if (m_writingNetPackets) if (m_writingNetPackets)
{ {
m_writingNetPackets = false; m_writingNetPackets = false;
jsonFile.EndArray(); auto& jsonFile = m_jsonFile;
jsonFile.Reset();
jsonFile.StartObject();
jsonFile.StartObject("netpackets_end");
jsonFile.WriteInt32("numTrailingBits", trailingBits.numTrailingBits);
jsonFile.WriteInt32("trailingBitsValue", trailingBits.value, (trailingBits.numTrailingBits > 0));
jsonFile.EndObject();
jsonFile.EndObject();
assert(jsonFile.IsComplete());
} }
jsonFile.EndObject();
jsonFile.EndObject();
} }
void JsonWriter::WriteNetPacket(NetPacket& packet, SourceGameContext& context) void JsonWriter::WriteNetPacket(NetPacket& packet, SourceGameContext& context)
{ {
auto& jsonFile = m_jsonFile; auto& jsonFile = m_jsonFile;
jsonFile.Reset();
jsonFile.StartObject(); jsonFile.StartObject();
NetHandlers::NetMsg_JsonWrite(packet.type, jsonFile, context, packet.data); NetHandlers::NetMsg_JsonWrite(packet.type, jsonFile, context, packet.data);
jsonFile.EndObject(); jsonFile.EndObject();
assert(jsonFile.IsComplete());
} }