Tencent / rapidjson

A fast JSON parser/generator for C++ with both SAX/DOM style API
http://rapidjson.org/
Other
14.16k stars 3.52k forks source link

RAPIDJSON_ASSERT #1606

Open RoyBellingan opened 4 years ago

RoyBellingan commented 4 years ago

I was looking for guidelines on how to configure the RAPIDJSON_ASSERT function but founded only -> https://github.com/Tencent/rapidjson/issues/1004 that basically says "use of exception should be fine"

The basic idea is to have something like thread_local bool rapidAssertEnabled = true; .... void rapidAssert(bool condition) { if (!condition && rapidAssertEnabled) { // custom function based on https://github.com/bombela/backward-cpp to print stack trace of the error throw something; } }

Because I tried to ... just ignore the check and return always true to perform operation like auto f1 = json["element"][3]["id"].GetString(); And just check if f1 is set as something or not, instead of checking the whole chain...

I think the correct answer is "just use json pointer !"

But I am curious to know if this is an intentional choice or not, I tested many condition, iterated thousand of times, runned under valgrind / libasan and is always perfect everything...

So I can easily toggle the soft fail with default value mode, or a pedantic check mode that return me the exact line and reason of an error.

Am I crazy ?!?

miloyip commented 4 years ago

I think we try to let the user to choose whatever he wants to. Assertion is for checking invalid usage in the programmer side, it was not intended for runtime checking at first. Using thread local flag is possible, but it also incur a little bit performance overhead.

RoyBellingan commented 4 years ago

That is exactly what I was hoping for! Excellent! (About overhead do not worry, code will be statically compiled so is preallocated and is the same thing as a normal stack based variable http://david-grs.github.io/tls_performance_overhead_cost_linux/)