LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
parser::JsonParser Class Reference

#include <JsonParser.hpp>

Inheritance diagram for parser::JsonParser:
Inheritance graph
Collaboration diagram for parser::JsonParser:
Collaboration graph

Public Member Functions

 JsonParser ()=default
 
 ~JsonParser () override=default
 
void ParseData (const std::filesystem::path &filepath) override
 Parses data from a file path.
 
void ParseData (std::istream &input) override
 Parses data from an input stream.
 
uint32_t GetCurrentProgress () const override
 Gets the current parsing progress.
 
uint32_t GetTotalProgress () const override
 Gets the total expected progress value.
 
virtual std::vector< std::pair< std::string, std::string > > GetFileMetadata () const
 Ordered key → value pairs read from the file header during the last ParseData() call (see db::EventsContainer::SetFileMetadata).
 
void RegisterObserver (IDataParserObserver *observer)
 Registers an observer to receive parsing notifications.
 
void UnregisterObserver (IDataParserObserver *observer)
 
void NotifyNewEvent (db::LogEvent &&event)
 
void NotifyProgressUpdated ()
 
void NotifyNewEventBatch (std::vector< std::pair< int, db::LogEvent::EventItems > > &&eventBatch)
 

Private Member Functions

void Flatten (const nlohmann::json &node, const std::string &prefix, db::LogEvent::EventItems &out) const
 

Private Attributes

uint32_t m_currentProgress {0}
 
uint32_t m_totalProgress {0}
 
std::vector< IDataParserObserver * > observers
 

Detailed Description

Parses JSON log files in two formats:

Array — a single JSON array of objects at the top level. [ {"ts": "...", "level": "INFO", "msg": "..."}, ... ]

NDJSON — one JSON object per line (newline-delimited JSON / JSONL). {"ts": "...", "level": "INFO", "msg": "..."} {"ts": "...", "level": "WARN", "msg": "..."}

The format is detected automatically: the parser reads the first non-whitespace character; '[' means array mode, '{' means NDJSON mode.

Nested objects and arrays are flattened using dot-notation keys: { "ctx": { "user": "alice" } } → key "ctx.user" = "alice" { "tags": ["a","b"] } → key "tags.0" = "a", "tags.1" = "b"

Constructor & Destructor Documentation

◆ JsonParser()

parser::JsonParser::JsonParser ( )
default

◆ ~JsonParser()

parser::JsonParser::~JsonParser ( )
overridedefault

Member Function Documentation

◆ Flatten()

void parser::JsonParser::Flatten ( const nlohmann::json &  node,
const std::string &  prefix,
db::LogEvent::EventItems &  out 
) const
private
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetCurrentProgress()

uint32_t parser::JsonParser::GetCurrentProgress ( ) const
inlineoverridevirtual

Gets the current parsing progress.

Returns
uint32_t Current progress value (meaning depends on implementation)
Note
Progress values should be used with GetTotalProgress() for percentage calculations

Implements parser::IDataParser.

◆ GetFileMetadata()

virtual std::vector< std::pair< std::string, std::string > > parser::IDataParser::GetFileMetadata ( ) const
inlinevirtualinherited

Ordered key → value pairs read from the file header during the last ParseData() call (see db::EventsContainer::SetFileMetadata).

Returns
Empty for formats without a header (the default).

Reimplemented in parser::SapiLogParser.

◆ GetTotalProgress()

uint32_t parser::JsonParser::GetTotalProgress ( ) const
inlineoverridevirtual

Gets the total expected progress value.

Returns
uint32_t Maximum progress value when parsing is complete
Note
Return 0 if total progress cannot be determined in advance

Implements parser::IDataParser.

◆ NotifyNewEvent()

void parser::IDataParser::NotifyNewEvent ( db::LogEvent &&  event)
inlineinherited

◆ NotifyNewEventBatch()

void parser::IDataParser::NotifyNewEventBatch ( std::vector< std::pair< int, db::LogEvent::EventItems > > &&  eventBatch)
inlineinherited
Here is the caller graph for this function:

◆ NotifyProgressUpdated()

void parser::IDataParser::NotifyProgressUpdated ( )
inlineinherited
Here is the caller graph for this function:

◆ ParseData() [1/2]

void parser::JsonParser::ParseData ( const std::filesystem::path &  filepath)
overridevirtual

Parses data from a file path.

Parameters
filepathThe path to the file to parse
Exceptions
std::runtime_errorif the file cannot be opened or parsed
Note
This method should be implemented by derived classes for file-based parsing

Implements parser::IDataParser.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ParseData() [2/2]

void parser::JsonParser::ParseData ( std::istream &  input)
overridevirtual

Parses data from an input stream.

Parameters
inputThe input stream to read data from
Exceptions
std::runtime_errorif the stream cannot be read or parsed
Note
This method enables parsing from various sources (files, network, memory)

Implements parser::IDataParser.

Here is the call graph for this function:

◆ RegisterObserver()

void parser::IDataParser::RegisterObserver ( IDataParserObserver *  observer)
inlineinherited

Registers an observer to receive parsing notifications.

Parameters
observerPointer to the observer object
Note
The observer must remain valid until parsing completes or it's unregistered
Duplicate observers are automatically prevented

◆ UnregisterObserver()

void parser::IDataParser::UnregisterObserver ( IDataParserObserver *  observer)
inlineinherited

Member Data Documentation

◆ m_currentProgress

uint32_t parser::JsonParser::m_currentProgress {0}
private

◆ m_totalProgress

uint32_t parser::JsonParser::m_totalProgress {0}
private

◆ observers

std::vector<IDataParserObserver*> parser::IDataParser::observers
privateinherited

The documentation for this class was generated from the following files: