Improve RecordVal JSON formatting

No need to create a record introspection table each time when all
the required information can be obtained directly in the RecordVal
and RecordType objects.  Besides the additional overhead, using such
a table will re-order the fields arbitrarily instead of using the
order in which they're defined.
This commit is contained in:
Jon Siwek 2019-09-30 19:04:55 -07:00
parent d258ebe5ca
commit a34ade4876
4 changed files with 25 additions and 24 deletions

View file

@ -582,37 +582,32 @@ static ZeekJson BuildJSON(Val* val, bool only_loggable=false, RE_Matcher* re=new
{
j = ZeekJson::object();
auto* rval = val->AsRecordVal();
TableVal* fields = rval->GetRecordFields();
auto* field_indexes = fields->ConvertToPureList();
int num_indexes = field_indexes->Length();
auto rt = rval->Type()->AsRecordType();
for ( int i = 0; i < num_indexes; ++i )
for ( auto i = 0; i < rt->NumFields(); ++i )
{
Val* key = field_indexes->Index(i);
auto* key_field = fields->Lookup(key)->AsRecordVal();
auto field_name = rt->FieldName(i);
std::string key_string;
auto* key_val = key->AsStringVal();
string key_string;
if ( re->MatchAnywhere(key_val->AsString()) != 0 )
if ( re->MatchAnywhere(field_name) != 0 )
{
StringVal blank("");
key_val = key_val->Substitute(re, &blank, 0)->AsStringVal();
StringVal fn_val(field_name);
auto key_val = fn_val.Substitute(re, &blank, 0)->AsStringVal();
key_string = key_val->ToStdString();
delete key_val;
Unref(key_val);
}
else
key_string = key_val->ToStdString();
key_string = field_name;
Val* value = key_field->Lookup("value", true);
Val* value = rval->LookupWithDefault(i);
if ( value && ( ! only_loggable || key_field->Lookup("log")->AsBool() ) )
if ( value && ( ! only_loggable || rt->FieldHasAttr(i, ATTR_LOG) ) )
j[key_string] = BuildJSON(value, only_loggable, re);
Unref(value);
}
delete fields;
delete field_indexes;
break;
}