Use json::emplace to avoid some extra calls to operator[]

This commit is contained in:
Tim Wojtulewicz 2019-09-25 16:12:50 -07:00
parent c8f2d52d91
commit d4d1009b5c
4 changed files with 12 additions and 12 deletions

View file

@ -494,8 +494,8 @@ static ZeekJson BuildJSON(Val* val, bool only_loggable=false, RE_Matcher* re=new
case TYPE_PORT: case TYPE_PORT:
{ {
auto* pval = val->AsPortVal(); auto* pval = val->AsPortVal();
j["port"] = pval->Port(); j.emplace("port", pval->Port());
j["proto"] = pval->Protocol(); j.emplace("proto", pval->Protocol());
break; break;
} }
@ -577,7 +577,7 @@ static ZeekJson BuildJSON(Val* val, bool only_loggable=false, RE_Matcher* re=new
else else
key_string = key_json.dump(); key_string = key_json.dump();
j[key_string] = BuildJSON(entry_value, only_loggable, re); j.emplace(key_string, BuildJSON(entry_value, only_loggable, re));
} }
Unref(lv); Unref(lv);
@ -614,7 +614,7 @@ static ZeekJson BuildJSON(Val* val, bool only_loggable=false, RE_Matcher* re=new
Val* value = key_field->Lookup("value", true); Val* value = key_field->Lookup("value", true);
if ( value && ( ! only_loggable || key_field->Lookup("log")->AsBool() ) ) if ( value && ( ! only_loggable || key_field->Lookup("log")->AsBool() ) )
j[key_string] = BuildJSON(value, only_loggable, re); j.emplace(key_string, BuildJSON(value, only_loggable, re));
} }
delete fields; delete fields;
@ -646,9 +646,8 @@ static ZeekJson BuildJSON(Val* val, bool only_loggable=false, RE_Matcher* re=new
case TYPE_OPAQUE: case TYPE_OPAQUE:
{ {
j = ZeekJson::object();
auto* oval = val->AsOpaqueVal(); auto* oval = val->AsOpaqueVal();
j["opaque_type"] = OpaqueMgr::mgr()->TypeID(oval); j = { { "opaque_type", OpaqueMgr::mgr()->TypeID(oval) } };
break; break;
} }

View file

@ -37,7 +37,7 @@ bool JSON::Describe(ODesc* desc, int num_fields, const Field* const * fields,
if ( new_entry.is_null() ) if ( new_entry.is_null() )
return false; return false;
j[fields[i]->name] = new_entry; j.emplace(fields[i]->name, new_entry);
} }
} }
@ -182,11 +182,7 @@ ZeekJson JSON::BuildJSON(Value* val, const string& name) const
} }
if ( ! name.empty() && ! j.is_null() ) if ( ! name.empty() && ! j.is_null() )
{ return { { name, j } };
ZeekJson j2 = ZeekJson::object();
j2[name] = j;
return j2;
}
return j; return j;
} }

View file

@ -38,3 +38,4 @@ true
{"10.1.1.1":{"a":1},"10.2.2.2":{"b":2}} {"10.1.1.1":{"a":1},"10.2.2.2":{"b":2}}
{"10.1.1.1":[1,2],"10.2.2.2":[3,5]} {"10.1.1.1":[1,2],"10.2.2.2":[3,5]}
{"1":{"s":"test"}} {"1":{"s":"test"}}
{"opaque_type":"TopkVal"}

View file

@ -122,4 +122,8 @@ event zeek_init()
print to_json(ta3); print to_json(ta3);
print to_json(ta4); print to_json(ta4);
print to_json(ta5, T); print to_json(ta5, T);
# Opaque
local o1: opaque of topk = topk_init(5);
print to_json(o1);
} }