deploy: 94fe31eb9b
This commit is contained in:
1 parent
41c601a7d1
commit
d2e264bd84
1400 files changed
+11859
-5440
No files matched your search
+42
-33
@@ -1,4 +1,4 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/home/exceptions/" rel="canonical"><link href=../faq/ rel=prev><link href=../releases/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Exceptions - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../css/custom.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#exceptions class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Exceptions </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> <div class=md-search data-md-componLine truncated
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/home/exceptions/" rel="canonical"><link href=../faq/ rel=prev><link href=../releases/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>Exceptions - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../css/custom.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#exceptions class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Exceptions </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> <div class=md-search data-md-componLine truncated
|
||||
direction LR
|
||||
class `std::exception` {
|
||||
<<interface>>
|
||||
@@ -23,7 +23,7 @@
|
||||
`json::exception` <|-- `json::invalid_iterator`
|
||||
`json::exception` <|-- `json::type_error`
|
||||
`json::exception` <|-- `json::out_of_range`
|
||||
`json::exception` <|-- `json::other_error`</code></pre> <h3 id=switch-off-exceptions>Switch off exceptions<a class=headerlink href=#switch-off-exceptions title="Permanent link">¶</a></h3> <p>Exceptions are used widely within the library. They can, however, be switched off with either using the compiler flag <code>-fno-exceptions</code> or by defining the symbol <a href=../../api/macros/json_noexception/ ><code>JSON_NOEXCEPTION</code></a>. In this case, exceptions are replaced by <code>abort()</code> calls. You can further control this behavior by defining <code>JSON_THROW_USER</code> (overriding <code class=highlight><span class=k>throw</span></code>), <code>JSON_TRY_USER</code> (overriding <code class=highlight><span class=k>try</span></code>), and <code>JSON_CATCH_USER</code> (overriding <code class=highlight><span class=k>catch</span></code>).</p> <p>Note that <a href=../../api/macros/json_throw_user/ ><code>JSON_THROW_USER</code></a> should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior.</p> <details class=example> <summary>Example: switch off exceptions and log errors before aborting</summary> <p>The code below switches off exceptions and creates a log entry with a detailed error message in case of errors.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
`json::exception` <|-- `json::other_error`</code></pre> <h3 id=switch-off-exceptions>Switch off exceptions<a class=headerlink href=#switch-off-exceptions title="Permanent link">¶</a></h3> <p>Exceptions are used widely within the library. They can, however, be switched off with either using the compiler flag <code>-fno-exceptions</code> or by defining the symbol <a href=../../api/macros/json_noexception/ ><code>JSON_NOEXCEPTION</code></a>. In this case, exceptions are replaced by <code>abort()</code> calls. You can further control this behavior by defining <code>JSON_THROW_USER</code> (overriding <code class=highlight><span class=k>throw</span></code>), <code>JSON_TRY_USER</code> (overriding <code class=highlight><span class=k>try</span></code>), and <code>JSON_CATCH_USER</code> (overriding <code class=highlight><span class=k>catch</span></code>).</p> <p>See <a href=../../features/no_exceptions/ >Working without exceptions</a> for the functions that report errors without throwing.</p> <p>Note that <a href=../../api/macros/json_throw_user/ ><code>JSON_THROW_USER</code></a> should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior.</p> <details class=example> <summary>Example: switch off exceptions and log errors before aborting</summary> <p>The code below switches off exceptions and creates a log entry with a detailed error message in case of errors.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
|
||||
<span class=cp>#define JSON_TRY_USER if(true)</span>
|
||||
<span class=cp>#define JSON_CATCH_USER(exception) if(false)</span>
|
||||
@@ -37,7 +37,7 @@
|
||||
</code></pre></div> </details> <p>Note the explanatory <a href="https://en.cppreference.com/w/cpp/error/exception/what"><code>what()</code></a> string of exceptions is not available for <abbr title="Microsoft Visual C++">MSVC</abbr> if exceptions are disabled, see <a href="https://github.com/nlohmann/json/discussions/2824">#2824</a>.</p> <p>See <a href=../../api/macros/json_throw_user/ >documentation of <code>JSON_TRY_USER</code>, <code>JSON_CATCH_USER</code> and <code>JSON_THROW_USER</code></a> for more information.</p> <h3 id=extended-diagnostic-messages>Extended diagnostic messages<a class=headerlink href=#extended-diagnostic-messages title="Permanent link">¶</a></h3> <p>Exceptions in the library are thrown in the local context of the <abbr title="JavaScript Object Notation">JSON</abbr> value they are detected. This makes detailed diagnostics messages, and hence debugging, difficult.</p> <details class=example> <summary>Example: standard diagnostic message</summary> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
@@ -60,7 +60,7 @@
|
||||
<span class=cp># define JSON_DIAGNOSTICS 1</span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
@@ -81,7 +81,7 @@
|
||||
</code></pre></div> <p>Now the exception message contains a <abbr title="JavaScript Object Notation">JSON</abbr> Pointer <code>/address/housenumber</code> that indicates which value has the wrong type.</p> </details> <p>See <a href=../../api/macros/json_diagnostics/ >documentation of <code>JSON_DIAGNOSTICS</code></a> for more information, and <a href=../debugging/ >Debugging</a> for the debugger integrations of the library.</p> <h2 id=parse-errors>Parse errors<a class=headerlink href=#parse-errors title="Permanent link">¶</a></h2> <p>The library throws this exception when a parse error occurs. Parse errors can occur during the deserialization of <abbr title="JavaScript Object Notation">JSON</abbr> text or of one of the binary formats (<abbr title="Binary JData">BJData</abbr>, <abbr title="Binary Object Notation 8">BON8</abbr>, <abbr title="Binary JSON">BSON</abbr>, <abbr title="Concise Binary Object Representation">CBOR</abbr>, MessagePack, <abbr title="Universal Binary JSON">UBJSON</abbr>), as well as when using <abbr title="JavaScript Object Notation">JSON</abbr> Patch.</p> <p>Exceptions have ids 1xx.</p> <div class="admonition info"> <p class=admonition-title>Byte index</p> <p>Member <code>byte</code> holds the byte index of the last read character in the input file.</p> <p>For an input with n bytes, 1 is the index of the first character and n+1 is the index of the terminating null byte or the end of file. This also holds true when reading a byte vector (<abbr title="Concise Binary Object Representation">CBOR</abbr> or MessagePack).</p> </div> <details class=example> <summary>Example: catch a <code>parse_error</code> exception</summary> <p>The following code shows how a <code>parse_error</code> exception can be caught.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
@@ -95,7 +95,7 @@
|
||||
<span class=w> </span><span class=c1>// output exception information</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"message: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>what</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span>
|
||||
<span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"exception id: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>id</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span>
|
||||
<span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"byte position of error: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=kt>byte</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>endl</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"byte position of error: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=kt>byte</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code>message: [json.exception.parse_error.101] parse error at line 1, column 8: syntax error while parsing value - unexpected ']'; expected '[', '{', or a literal
|
||||
@@ -109,7 +109,14 @@ byte position of error: 8
|
||||
</code></pre></div> <p><code>\u</code> was not be followed by four hex digits:</p> <div class=highlight><pre><span></span><code>[json.exception.parse_error.101] parse error at line 1, column 6: syntax error while parsing value - invalid string: '\u' must be followed by 4 hex digits; last read: '"\u01"'
|
||||
</code></pre></div> <p>Invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8 surrogate pair:</p> <div class=highlight><pre><span></span><code>[json.exception.parse_error.101] parse error at line 1, column 13: syntax error while parsing value - invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF; last read: '"\uD7FF\uDC00'"
|
||||
</code></pre></div> <p>Invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8 byte:</p> <div class=highlight><pre><span></span><code>[json.exception.parse_error.101] parse error at line 3, column 24: syntax error while parsing value - invalid string: ill-formed UTF-8 byte; last read: '"vous \352t'
|
||||
</code></pre></div> </div> <div class="admonition tip"> <p class=admonition-title>Tip</p> <ul> <li>Make sure the input is correctly read. Try to write the input to standard output to check if, for instance, the input file was successfully opened.</li> <li>Paste the input to a <abbr title="JavaScript Object Notation">JSON</abbr> validator like <a href="https://jsonlint.com">https://jsonlint.com</a> or a tool like <a href="https://jqlang.github.io/jq/">jq</a>.</li> </ul> </div> <h3 id=jsonexceptionparse_error102>json.exception.parse_error.102<a class=headerlink href=#jsonexceptionparse_error102 title="Permanent link">¶</a></h3> <p><abbr title="JavaScript Object Notation">JSON</abbr> uses the <code>\uxxxx</code> format to describe Unicode characters. Code points above 0xFFFF are split into two <code>\uxxxx</code> entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>parse error at 14: missing or wrong low surrogate
|
||||
</code></pre></div> </div> <div class="admonition tip"> <p class=admonition-title>Tip</p> <ul> <li>Make sure the input is correctly read. Try to write the input to standard output to check if, for instance, the input file was successfully opened.</li> <li>Paste the input to a <abbr title="JavaScript Object Notation">JSON</abbr> validator like <a href="https://jsonlint.com">https://jsonlint.com</a> or a tool like <a href="https://jqlang.github.io/jq/">jq</a>.</li> </ul> </div> <details class=info> <summary>Parsing a file fails at line 1, column 1 (unexpected end of input)</summary> <p>The messages <code>attempting to parse an empty input</code> (since version 3.11.3) and <code>unexpected end of input</code> (also the message of earlier versions for an empty input) at the very start of the input mean that the parser did not see a single character. The library only reads from the stream it is given: it does not open files and cannot tell a missing file from an empty one. The usual causes are:</p> <ul> <li><strong>The file was not opened.</strong> A relative path is resolved against the <em>working directory of the running program</em>, not against the source file or the project. IDEs and build tools often start the program in a build directory (for example <code>build/</code> or <code>cmake-build-debug/</code>), so <code>"example.json"</code> may need to be <code>"../example.json"</code>. Use an absolute path to rule this out.</li> <li><strong>The file is empty</strong>, or a previous run truncated it (for example when it was opened for writing).</li> <li><strong>The stream was already read.</strong> A stream whose content has been consumed (by printing it with <code>std::cout << file.rdbuf()</code>, or by an earlier <code>accept</code>/<code>parse</code>/<code>>></code>) is at its end. Parse it once, or rewind it with <code>seekg(0)</code> after clearing the state with <code>clear()</code>.</li> </ul> <p>Check the stream before parsing it:</p> <div class=highlight><pre><span></span><code><span class=n>std</span><span class=o>::</span><span class=n>ifstream</span><span class=w> </span><span class=n>f</span><span class=p>(</span><span class=s>"example.json"</span><span class=p>);</span>
|
||||
<span class=k>if</span><span class=w> </span><span class=p>(</span><span class=o>!</span><span class=n>f</span><span class=p>.</span><span class=n>is_open</span><span class=p>())</span>
|
||||
<span class=p>{</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cerr</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"cannot open example.json</span><span class=se>\n</span><span class=s>"</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=k>return</span><span class=w> </span><span class=mi>1</span><span class=p>;</span>
|
||||
<span class=p>}</span>
|
||||
<span class=n>json</span><span class=w> </span><span class=n>data</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=n>f</span><span class=p>);</span>
|
||||
</code></pre></div> </details> <h3 id=jsonexceptionparse_error102>json.exception.parse_error.102<a class=headerlink href=#jsonexceptionparse_error102 title="Permanent link">¶</a></h3> <p><abbr title="JavaScript Object Notation">JSON</abbr> uses the <code>\uxxxx</code> format to describe Unicode characters. Code points above 0xFFFF are split into two <code>\uxxxx</code> entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>parse error at 14: missing or wrong low surrogate
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception is not used any more. Instead <a href=#jsonexceptionparse_error101>json.exception.parse_error.101</a> with a more detailed description is used.</p> </div> <h3 id=jsonexceptionparse_error103>json.exception.parse_error.103<a class=headerlink href=#jsonexceptionparse_error103 title="Permanent link">¶</a></h3> <p>Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>parse error: code points above 0x10FFFF are invalid
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception is not used any more. Instead <a href=#jsonexceptionparse_error101>json.exception.parse_error.101</a> with a more detailed description is used.</p> </div> <h3 id=jsonexceptionparse_error104>json.exception.parse_error.104<a class=headerlink href=#jsonexceptionparse_error104 title="Permanent link">¶</a></h3> <p><a href="https://tools.ietf.org/html/rfc6902"><abbr title="Request for Comments">RFC</abbr> 6902</a> requires a <abbr title="JavaScript Object Notation">JSON</abbr> Patch document to be a <abbr title="JavaScript Object Notation">JSON</abbr> document that represents an array of objects.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.parse_error.104] parse error: JSON patch must be an array of objects
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionparse_error105>json.exception.parse_error.105<a class=headerlink href=#jsonexceptionparse_error105 title="Permanent link">¶</a></h3> <p>An operation of a <abbr title="JavaScript Object Notation">JSON</abbr> Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <p><div class=highlight><pre><span></span><code>[json.exception.parse_error.105] parse error: operation 'add' must have member 'value'
|
||||
@@ -120,29 +127,31 @@ byte position of error: 8
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionparse_error108>json.exception.parse_error.108<a class=headerlink href=#jsonexceptionparse_error108 title="Permanent link">¶</a></h3> <p>In a <abbr title="JavaScript Object Notation">JSON</abbr> Pointer, only <code>~0</code> and <code>~1</code> are valid escape sequences.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '1'
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionparse_error109>json.exception.parse_error.109<a class=headerlink href=#jsonexceptionparse_error109 title="Permanent link">¶</a></h3> <p>A <abbr title="JavaScript Object Notation">JSON</abbr> Pointer array index must be a number. This exception is thrown for an array index that does not begin with a digit, except for <code>-</code> and the empty reference token, which throw <a href=#jsonexceptionout_of_range404><code>out_of_range.404</code></a> where they cannot be resolved.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>[json.exception.parse_error.109] parse error: array index 'one' is not a number
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.109] parse error: array index '+1' is not a number
|
||||
</code></pre></div></p> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>Before version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, a one-character array index that is not a digit (e.g., <code>x</code>) threw <a href=#jsonexceptionout_of_range404><code>out_of_range.404</code></a> instead.</p> </div> <h3 id=jsonexceptionparse_error110>json.exception.parse_error.110<a class=headerlink href=#jsonexceptionparse_error110 title="Permanent link">¶</a></h3> <p>When parsing a <a href=../../features/binary_formats/ >binary format</a>, the byte vector ends before the complete value has been read.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: unexpected end of input
|
||||
</code></pre></div></p> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>Before version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, a one-character array index that is not a digit (e.g., <code>x</code>) threw <a href=#jsonexceptionout_of_range404><code>out_of_range.404</code></a> instead.</p> </div> <h3 id=jsonexceptionparse_error110>json.exception.parse_error.110<a class=headerlink href=#jsonexceptionparse_error110 title="Permanent link">¶</a></h3> <p>When parsing a <a href=../../features/binary_formats/ >binary format</a>, the byte vector ends before the complete value has been read, or (with <code>strict = true</code>) further bytes follow the value.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: unexpected end of input
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing UBJSON value: expected end of input; last byte: 0x5A
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing BSON number: unexpected end of input
|
||||
</code></pre></div></p> </div> <h3 id=jsonexceptionparse_error112>json.exception.parse_error.112<a class=headerlink href=#jsonexceptionparse_error112 title="Permanent link">¶</a></h3> <p>An unexpected byte was read in a <a href=../../features/binary_formats/ >binary format</a> or length information is invalid (<a href=../../features/binary_formats/bson/ ><abbr title="Binary JSON">BSON</abbr></a>).</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing CBOR value: invalid byte: 0x1C
|
||||
</code></pre></div></p> </div> <h3 id=jsonexceptionparse_error112>json.exception.parse_error.112<a class=headerlink href=#jsonexceptionparse_error112 title="Permanent link">¶</a></h3> <p>An unexpected byte was read in a <a href=../../features/binary_formats/ >binary format</a> or length information is invalid (<a href=../../features/binary_formats/bson/ ><abbr title="Binary JSON">BSON</abbr></a>). In <a href=../../features/binary_formats/bon8/ ><abbr title="Binary Object Notation 8">BON8</abbr></a>, this exception is also thrown for a key that is not a string and for ill-formed <abbr title="Unicode Transformation Format">UTF</abbr>-8.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing CBOR value: invalid byte: 0x1C
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.112] parse error at byte 1: syntax error while parsing MessagePack value: invalid byte: 0xC1
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.112] parse error at byte 4: syntax error while parsing BJData size: expected '#' after type information; last byte: 0x02
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.112] parse error at byte 4: syntax error while parsing UBJSON size: expected '#' after type information; last byte: 0x02
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.112] parse error at byte 10: syntax error while parsing BSON string: string length must be at least 1, is -2147483648
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.112] parse error at byte 15: syntax error while parsing BSON binary: byte array length cannot be negative, is -1
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.112] parse error at byte 5: syntax error while parsing BSON document: document size 6 does not match the number of bytes read (5)
|
||||
</code></pre></div></p> </div> <h3 id=jsonexceptionparse_error113>json.exception.parse_error.113<a class=headerlink href=#jsonexceptionparse_error113 title="Permanent link">¶</a></h3> <p>A string could not be read from a <a href=../../features/binary_formats/ >binary format</a>: either a value that is not a string was read where one was required (for instance as a map key), the string's length specification is invalid, or the string's bytes are not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and the <code>error_handler</code> parameter of the corresponding <code>from_*</code> function is set to <code>strict</code>. By default (<code>error_handler_t::keep</code>), the bytes of a string are not checked for valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 on read; see the ill-formed <abbr title="Unicode Transformation Format">UTF</abbr>-8 notes on the individual <a href=../../features/binary_formats/ >binary format</a> pages for how such a string is handled depending on <code>error_handler</code>.</p> <p><abbr title="Concise Binary Object Representation">CBOR</abbr> and MessagePack allow map keys of any type, but <abbr title="JavaScript Object Notation">JSON</abbr> object keys are always strings. Maps with keys of any other type (for instance integers or <code>null</code>) are therefore not supported; see the notes on <a href=../../features/binary_formats/cbor/ ><abbr title="Concise Binary Object Representation">CBOR</abbr></a> and <a href=../../features/binary_formats/messagepack/ >MessagePack</a>.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR object key: only string keys are supported, but found an unsigned integer; last byte: 0x01
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.112] parse error at byte 2: syntax error while parsing BON8 string: invalid UTF-8 byte: 0x80
|
||||
</code></pre></div></p> </div> <h3 id=jsonexceptionparse_error113>json.exception.parse_error.113<a class=headerlink href=#jsonexceptionparse_error113 title="Permanent link">¶</a></h3> <p>A string could not be read from a <a href=../../features/binary_formats/ >binary format</a>: either a value that is not a string was read where one was required (for instance as a map key), the string's length specification is invalid, or the string's bytes are not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 and the <code>error_handler</code> parameter of the corresponding <code>from_*</code> function is set to <code>strict</code>. By default (<code>error_handler_t::keep</code>), the bytes of a string are not checked for valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 on read. This applies to the <code>from_*</code> functions that have an <code>error_handler</code> parameter; <code>from_bon8</code> does not have one. See the ill-formed <abbr title="Unicode Transformation Format">UTF</abbr>-8 notes on the individual <a href=../../features/binary_formats/ >binary format</a> pages for how such a string is handled depending on <code>error_handler</code>. <a href=../../api/basic_json/from_bon8/ ><code>from_bon8()</code></a> never throws this exception: <abbr title="Binary Object Notation 8">BON8</abbr> non-string keys and ill-formed <abbr title="Unicode Transformation Format">UTF</abbr>-8 are reported as <a href=#jsonexceptionparse_error112>parse_error.112</a>.</p> <p><abbr title="Concise Binary Object Representation">CBOR</abbr> and MessagePack allow map keys of any type, but <abbr title="JavaScript Object Notation">JSON</abbr> object keys are always strings. Maps with keys of any other type (for instance integers or <code>null</code>) are therefore not supported; see the notes on <a href=../../features/binary_formats/cbor/ ><abbr title="Concise Binary Object Representation">CBOR</abbr></a> and <a href=../../features/binary_formats/messagepack/ >MessagePack</a>.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR object key: only string keys are supported, but found an unsigned integer; last byte: 0x01
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing MessagePack object key: only string keys are supported, but found nil; last byte: 0xC0
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x7C
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON char: byte after 'C' must be in range 0x00..0x7F; last byte: 0x82
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData string: string length must not be negative
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing CBOR string: invalid string: ill-formed UTF-8 byte
|
||||
</code></pre></div></p> </div> <h3 id=jsonexceptionparse_error114>json.exception.parse_error.114<a class=headerlink href=#jsonexceptionparse_error114 title="Permanent link">¶</a></h3> <p>The parsing of the corresponding <abbr title="Binary JSON">BSON</abbr> record type is not implemented (yet).</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.parse_error.114] parse error at byte 5: Unsupported BSON record type 0xFF
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionparse_error115>json.exception.parse_error.115<a class=headerlink href=#jsonexceptionparse_error115 title="Permanent link">¶</a></h3> <p>A <abbr title="Universal Binary JSON">UBJSON</abbr> high-precision number could not be parsed.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1A
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionparse_error116>json.exception.parse_error.116<a class=headerlink href=#jsonexceptionparse_error116 title="Permanent link">¶</a></h3> <p><a href=../../api/basic_json_document/load/ ><code>basic_json_document::load()</code></a> rejected an <a href=../../features/json_view/#images>image</a>: either the bytes are not one <a href=../../api/basic_json_document/save/ ><code>save()</code></a> could have written (too short, an unknown magic number or format version, or sizes that do not fit the buffer), or they are, but fail the requested <a href=../../api/basic_json_document/load/#image_check><code>image_check</code></a>.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.parse_error.116] parse error: invalid json_document image: the check failed
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionparse_error115>json.exception.parse_error.115<a class=headerlink href=#jsonexceptionparse_error115 title="Permanent link">¶</a></h3> <p>A <abbr title="Universal Binary JSON">UBJSON</abbr> or <abbr title="Binary JData">BJData</abbr> high-precision number could not be parsed.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1A
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing BJData high-precision number: invalid number text: 1A
|
||||
</code></pre></div></p> </div> <h3 id=jsonexceptionparse_error116>json.exception.parse_error.116<a class=headerlink href=#jsonexceptionparse_error116 title="Permanent link">¶</a></h3> <p><a href=../../api/basic_json_document/load/ ><code>basic_json_document::load()</code></a> rejected an <a href=../../features/json_view/#images>image</a>: either the bytes are not one <a href=../../api/basic_json_document/save/ ><code>save()</code></a> could have written (too short, an unknown magic number or format version, or sizes that do not fit the buffer), or they are, but fail the requested <a href=../../api/basic_json_document/load/#image_check><code>image_check</code></a>.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.parse_error.116] parse error: invalid json_document image: the check failed
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, together with <a href=../../features/json_view/#images>images</a>.</p> </div> <h2 id=iterator-errors>Iterator errors<a class=headerlink href=#iterator-errors title="Permanent link">¶</a></h2> <p>This exception is thrown if iterators passed to a library function do not match the expected semantics.</p> <p>Exceptions have ids 2xx.</p> <details class=example> <summary>Example: catch an <code>invalid_iterator</code> exception</summary> <p>The following code shows how an <code>invalid_iterator</code> exception can be caught.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
@@ -157,7 +166,7 @@ byte position of error: 8
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// output exception information</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"message: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>what</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span>
|
||||
<span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"exception id: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>id</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>endl</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"exception id: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>id</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code>message: [json.exception.invalid_iterator.207] cannot use key() for non-object iterators
|
||||
@@ -180,7 +189,7 @@ exception id: 207
|
||||
</code></pre></div> </div> <h2 id=type-errors>Type errors<a class=headerlink href=#type-errors title="Permanent link">¶</a></h2> <p>This exception is thrown in case of a type error; that is, a library function is executed on a <abbr title="JavaScript Object Notation">JSON</abbr> value whose type does not match the expected semantics.</p> <p>Exceptions have ids 3xx.</p> <details class=example> <summary>Example: catch a <code>type_error</code> exception</summary> <p>The following code shows how a <code>type_error</code> exception can be caught.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
@@ -194,7 +203,7 @@ exception id: 207
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// output exception information</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"message: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>what</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span>
|
||||
<span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"exception id: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>id</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>endl</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"exception id: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>id</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code>message: [json.exception.type_error.308] cannot use push_back() with string
|
||||
@@ -220,18 +229,17 @@ exception id: 308
|
||||
</code></pre></div> </div> <h3 id=jsonexceptiontype_error313>json.exception.type_error.313<a class=headerlink href=#jsonexceptiontype_error313 title="Permanent link">¶</a></h3> <p>The <a href=../../api/basic_json/unflatten/ ><code>unflatten()</code></a> function converts an object whose keys are <abbr title="JavaScript Object Notation">JSON</abbr> Pointers back into an arbitrary nested <abbr title="JavaScript Object Notation">JSON</abbr> value. The <abbr title="JavaScript Object Notation">JSON</abbr> Pointers must not overlap, because then the resulting value would not be well-defined.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.type_error.313] invalid value to unflatten
|
||||
</code></pre></div> </div> <h3 id=jsonexceptiontype_error314>json.exception.type_error.314<a class=headerlink href=#jsonexceptiontype_error314 title="Permanent link">¶</a></h3> <p>The <a href=../../api/basic_json/unflatten/ ><code>unflatten()</code></a> function only works for an object whose keys are <abbr title="JavaScript Object Notation">JSON</abbr> Pointers.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <p>Calling <code>unflatten()</code> on an array <code class=highlight><span class=p>[</span><span class=mi>1</span><span class=p>,</span><span class=mi>2</span><span class=p>,</span><span class=mi>3</span><span class=p>]</span></code>:</p> <div class=highlight><pre><span></span><code>[json.exception.type_error.314] only objects can be unflattened
|
||||
</code></pre></div> </div> <h3 id=jsonexceptiontype_error315>json.exception.type_error.315<a class=headerlink href=#jsonexceptiontype_error315 title="Permanent link">¶</a></h3> <p>The <a href=../../api/basic_json/unflatten/ ><code>unflatten()</code></a> function only works for an object whose keys are <abbr title="JavaScript Object Notation">JSON</abbr> Pointers and whose values are primitive.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <p>Calling <code>unflatten()</code> on an object <code class=highlight><span class=p>{</span><span class=s2>"/1"</span><span class=p>,</span><span class=w> </span><span class=p>[</span><span class=mi>1</span><span class=p>,</span><span class=mi>2</span><span class=p>,</span><span class=mi>3</span><span class=p>]}</span></code>:</p> <div class=highlight><pre><span></span><code>[json.exception.type_error.315] values in object must be primitive
|
||||
</code></pre></div> </div> <h3 id=jsonexceptiontype_error316>json.exception.type_error.316<a class=headerlink href=#jsonexceptiontype_error316 title="Permanent link">¶</a></h3> <p>The <a href=../../api/basic_json/dump/ ><code>dump()</code></a> function only works with <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoded strings; that is, if you assign a <code>std::string</code> to a <abbr title="JavaScript Object Notation">JSON</abbr> value, make sure it is <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoded. See the <abbr title="Frequently Asked Questions">FAQ</abbr> entry on <a href=../faq/#serializing-untrusted-or-invalid-utf-8>serializing untrusted or invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8</a> for background and the recommended fix.</p> <p>The binary writers <a href=../../api/basic_json/to_cbor/ ><code>to_cbor()</code></a>, <a href=../../api/basic_json/to_ubjson/ ><code>to_ubjson()</code></a>, <a href=../../api/basic_json/to_bjdata/ ><code>to_bjdata()</code></a>, and <a href=../../api/basic_json/to_bson/ ><code>to_bson()</code></a> throw this exception as well for a string value or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 if their <code>error_handler</code> is <code>strict</code> (the default if <a href=../../api/macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled). So does <a href=../../api/basic_json/to_msgpack/ ><code>to_msgpack()</code></a> if <code>error_handler_t::strict</code> is passed.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <p>Calling <code>dump()</code> on a <abbr title="JavaScript Object Notation">JSON</abbr> value containing an <abbr title="International Organization for Standardization">ISO</abbr> 8859-1 encoded string: <div class=highlight><pre><span></span><code>[json.exception.type_error.316] invalid UTF-8 byte at index 15: 0x6F
|
||||
</code></pre></div> </div> <h3 id=jsonexceptiontype_error316>json.exception.type_error.316<a class=headerlink href=#jsonexceptiontype_error316 title="Permanent link">¶</a></h3> <p>The <a href=../../api/basic_json/dump/ ><code>dump()</code></a> function only works with <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoded strings; that is, if you assign a <code>std::string</code> to a <abbr title="JavaScript Object Notation">JSON</abbr> value, make sure it is <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoded. See the <abbr title="Frequently Asked Questions">FAQ</abbr> entry on <a href=../faq/#serializing-untrusted-or-invalid-utf-8>serializing untrusted or invalid <abbr title="Unicode Transformation Format">UTF</abbr>-8</a> for background and the recommended fix.</p> <p>The binary writers <a href=../../api/basic_json/to_cbor/ ><code>to_cbor()</code></a>, <a href=../../api/basic_json/to_ubjson/ ><code>to_ubjson()</code></a>, <a href=../../api/basic_json/to_bjdata/ ><code>to_bjdata()</code></a>, and <a href=../../api/basic_json/to_bson/ ><code>to_bson()</code></a> throw this exception as well for a string value or object key that is not valid <abbr title="Unicode Transformation Format">UTF</abbr>-8 if their <code>error_handler</code> is <code>strict</code> (the default if <a href=../../api/macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a> is enabled). So does <a href=../../api/basic_json/to_msgpack/ ><code>to_msgpack()</code></a> if <code>error_handler_t::strict</code> is passed. <a href=../../api/basic_json/to_bon8/ ><code>to_bon8()</code></a> always throws this exception for such a string or key, regardless of <a href=../../api/macros/json_strict_binary_utf8/ ><code>JSON_STRICT_BINARY_UTF8</code></a>, because the end of a <abbr title="Binary Object Notation 8">BON8</abbr> string is determined from its <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoding.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <p>Calling <code>dump()</code> on a <abbr title="JavaScript Object Notation">JSON</abbr> value containing an <abbr title="International Organization for Standardization">ISO</abbr> 8859-1 encoded string: <div class=highlight><pre><span></span><code>[json.exception.type_error.316] invalid UTF-8 byte at index 15: 0x6F
|
||||
</code></pre></div></p> </div> <div class="admonition tip"> <p class=admonition-title>Tip</p> <ul> <li>Store the source file with <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoding.</li> <li>Pass an error handler as last parameter to the <code>dump()</code> function to avoid this exception:<ul> <li><code>json::error_handler_t::replace</code> will replace invalid bytes sequences with <code>U+FFFD</code> </li> <li><code>json::error_handler_t::ignore</code> will silently ignore invalid byte sequences</li> <li><code>json::error_handler_t::keep</code> will copy invalid byte sequences to the output unchanged</li> </ul> </li> </ul> </div> <h3 id=jsonexceptiontype_error317>json.exception.type_error.317<a class=headerlink href=#jsonexceptiontype_error317 title="Permanent link">¶</a></h3> <p>The dynamic type of the object cannot be represented in the requested serialization format (e.g., a raw <code>true</code> or <code>null</code> <abbr title="JavaScript Object Notation">JSON</abbr> object cannot be serialized to <abbr title="Binary JSON">BSON</abbr>)</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p>Serializing <code class=highlight><span class=kc>null</span></code> to <abbr title="Binary JSON">BSON</abbr>: <div class=highlight><pre><span></span><code>[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is null
|
||||
</code></pre></div> Serializing <code class=highlight><span class=p>[</span><span class=mi>1</span><span class=p>,</span><span class=mi>2</span><span class=p>,</span><span class=mi>3</span><span class=p>]</span></code> to <abbr title="Binary JSON">BSON</abbr>: <div class=highlight><pre><span></span><code>[json.exception.type_error.317] to serialize to BSON, top-level type must be object, but is array
|
||||
</code></pre></div></p> </div> <div class="admonition tip"> <p class=admonition-title>Tip</p> <p>Encapsulate the <abbr title="JavaScript Object Notation">JSON</abbr> value in an object. That is, instead of serializing <code class=highlight><span class=kc>true</span></code>, serialize <code class=highlight><span class=p>{</span><span class=nt>"value"</span><span class=p>:</span><span class=w> </span><span class=kc>true</span><span class=p>}</span></code></p> </div> <h3 id=jsonexceptiontype_error318>json.exception.type_error.318<a class=headerlink href=#jsonexceptiontype_error318 title="Permanent link">¶</a></h3> <p>With <a href=../../api/macros/json_use_objects_for_enum_keyed_maps/ ><code>JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS</code></a>, a map with enum keys is stored as an object. This exception is thrown if two of its keys are converted to the same string, so one of the entries would be lost. This happens, for instance, if <a href=../../api/macros/nlohmann_json_serialize_enum/ ><code>NLOHMANN_JSON_SERIALIZE_ENUM</code></a> does not list an enumerator and it is therefore converted like the first listed one.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.type_error.318] duplicate object key 'red'
|
||||
</code></pre></div> </div> <h3 id=jsonexceptiontype_error319>json.exception.type_error.319<a class=headerlink href=#jsonexceptiontype_error319 title="Permanent link">¶</a></h3> <p><a href=../../api/basic_json_document/set/ ><code>basic_json_document::set</code></a>, <a href=../../api/basic_json_document/push_back/ ><code>basic_json_document::push_back</code></a>, and <a href=../../api/basic_json_document/insert/ ><code>basic_json_document::insert</code></a> can store any <code>basic_json</code> value except a binary one: a <code>json_document</code> has no representation for <a href=../../features/binary_values/ >binary values</a>, which only ever arise from parsing a binary format or from an explicit <a href=../../api/basic_json/binary/ ><code>json::binary</code></a> value, not from <abbr title="JavaScript Object Notation">JSON</abbr> text.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.type_error.319] cannot store a binary value in a json_document
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, together with editable <a href=../../features/json_view/ ><code>json_document</code>s</a>.</p> </div> <h3 id=jsonexceptiontype_error320>json.exception.type_error.320<a class=headerlink href=#jsonexceptiontype_error320 title="Permanent link">¶</a></h3> <p><a href=../../api/basic_json_document/save/ ><code>basic_json_document::save()</code></a> cannot write an <a href=../../features/json_view/#images>image</a> of a <a href=../../api/basic_json_document/is_discarded/ >discarded</a> document. <a href=../../api/basic_json_document/save/ ><code>save()</code></a> and <a href=../../api/basic_json_document/load/ ><code>load()</code></a> also throw this exception on a big-endian target, since the image format is little-endian only.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>[json.exception.type_error.320] cannot save a discarded json_document
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.type_error.320] json_document images need a little-endian target
|
||||
</code></pre></div></p> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, together with <a href=../../features/json_view/#images>images</a>.</p> </div> <h3 id=jsonexceptiontype_error321>json.exception.type_error.321<a class=headerlink href=#jsonexceptiontype_error321 title="Permanent link">¶</a></h3> <p>A discarded value (one created by <a href=../../api/basic_json/parse/ ><code>parse()</code></a> with a callback that returns <code>false</code> for the value, or by default-constructing a <a href=../../api/basic_json/ ><code>basic_json</code></a> with <a href=../../api/basic_json/value_t/ ><code>value_t::discarded</code></a>) was passed to a binary serialization function, either directly or nested in an array or object. There is no way to represent a discarded value in <abbr title="Concise Binary Object Representation">CBOR</abbr>, MessagePack, <abbr title="Universal Binary JSON">UBJSON</abbr>, <abbr title="Binary JData">BJData</abbr>, <abbr title="Binary JSON">BSON</abbr>, or <abbr title="Binary Object Notation 8">BON8</abbr>.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <p>Serializing <code class=highlight><span class=p>[</span><span class=mi>1</span><span class=p>,</span><span class=w> </span><span class=mi>2</span><span class=p>]</span></code> to <abbr title="Concise Binary Object Representation">CBOR</abbr>, where the second element was discarded by a parser callback: <div class=highlight><pre><span></span><code>[json.exception.type_error.321] cannot serialize discarded value to CBOR
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, together with editable <a href=../../features/json_view/ ><code>json_document</code>s</a>.</p> </div> <h3 id=jsonexceptiontype_error320>json.exception.type_error.320<a class=headerlink href=#jsonexceptiontype_error320 title="Permanent link">¶</a></h3> <p><a href=../../api/basic_json_document/save/ ><code>basic_json_document::save()</code></a> cannot write an <a href=../../features/json_view/#images>image</a> of a <a href=../../api/basic_json_document/is_discarded/ >discarded</a> document.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <div class=highlight><pre><span></span><code>[json.exception.type_error.320] cannot save a discarded json_document
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, together with <a href=../../features/json_view/#images>images</a>.</p> </div> <h3 id=jsonexceptiontype_error321>json.exception.type_error.321<a class=headerlink href=#jsonexceptiontype_error321 title="Permanent link">¶</a></h3> <p>A discarded value (one created by <a href=../../api/basic_json/parse/ ><code>parse()</code></a> with a callback that returns <code>false</code> for the value, or by default-constructing a <a href=../../api/basic_json/ ><code>basic_json</code></a> with <a href=../../api/basic_json/value_t/ ><code>value_t::discarded</code></a>) was passed to a binary serialization function, either directly or nested in an array or object. There is no way to represent a discarded value in <abbr title="Concise Binary Object Representation">CBOR</abbr>, MessagePack, <abbr title="Universal Binary JSON">UBJSON</abbr>, <abbr title="Binary JData">BJData</abbr>, <abbr title="Binary JSON">BSON</abbr>, or <abbr title="Binary Object Notation 8">BON8</abbr>.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <p>Serializing <code class=highlight><span class=p>[</span><span class=mi>1</span><span class=p>,</span><span class=w> </span><span class=mi>2</span><span class=p>]</span></code> to <abbr title="Concise Binary Object Representation">CBOR</abbr>, where the second element was discarded by a parser callback: <div class=highlight><pre><span></span><code>[json.exception.type_error.321] cannot serialize discarded value to CBOR
|
||||
</code></pre></div></p> </div> <h2 id=out-of-range>Out of range<a class=headerlink href=#out-of-range title="Permanent link">¶</a></h2> <p>This exception is thrown in case a library function is called on an input parameter that exceeds the expected range, for instance, in the case of array indices or nonexisting object keys.</p> <p>Exceptions have ids 4xx.</p> <details class=example> <summary>Example: catch an <code>out_of_range</code> exception</summary> <p>The following code shows how an <code>out_of_range</code> exception can be caught.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
<span class=p>{</span>
|
||||
@@ -245,7 +253,7 @@ exception id: 308
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// output exception information</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"message: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>what</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span>
|
||||
<span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"exception id: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>id</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>endl</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"exception id: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>id</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code>message: [json.exception.out_of_range.401] array index 4 is out of range
|
||||
@@ -257,28 +265,29 @@ exception id: 401
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionout_of_range405>json.exception.out_of_range.405<a class=headerlink href=#jsonexceptionout_of_range405 title="Permanent link">¶</a></h3> <p>The <abbr title="JavaScript Object Notation">JSON</abbr> Patch operations 'remove' and 'move' cannot be applied to the root element of the <abbr title="JavaScript Object Notation">JSON</abbr> value. The exception is also thrown when the parent or a reference token of an empty <abbr title="JavaScript Object Notation">JSON</abbr> Pointer is requested, for instance by <a href=../../api/json_pointer/back/ ><code>back</code></a> or <a href=../../api/json_pointer/pop_back/ ><code>pop_back</code></a>.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>JSON pointer has no parent
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionout_of_range406>json.exception.out_of_range.406<a class=headerlink href=#jsonexceptionout_of_range406 title="Permanent link">¶</a></h3> <p>A parsed number could not be stored without changing it to <abbr title="Not a Number">NaN</abbr> or INF. For the binary formats, this happens when a finite floating-point number does not fit into <a href=../../api/basic_json/number_float_t/ ><code>number_float_t</code></a>, for example a double-precision number when <code>number_float_t</code> is <code class=highlight><span class=kt>float</span></code>.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>number overflow parsing '10E1000'
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.out_of_range.406] syntax error while parsing CBOR value: number overflow
|
||||
</code></pre></div></p> </div> <h3 id=jsonexceptionout_of_range407>json.exception.out_of_range.407<a class=headerlink href=#jsonexceptionout_of_range407 title="Permanent link">¶</a></h3> <p>An integer number cannot be represented by the binary format it is serialized to:</p> <ul> <li><a href=../../features/binary_formats/bon8/ ><abbr title="Binary Object Notation 8">BON8</abbr></a> only stores integers that fit into int64, so <a href=../../api/basic_json/to_bon8/ ><code>to_bon8</code></a> throws for unsigned integers greater than 9223372036854775807.</li> <li><a href=../../features/binary_formats/cbor/ ><abbr title="Concise Binary Object Representation">CBOR</abbr></a>, <a href=../../features/binary_formats/messagepack/ >MessagePack</a>, and <a href=../../features/binary_formats/bson/ ><abbr title="Binary JSON">BSON</abbr></a> store integers in at most 64 bits. With the default number types, every integer fits, but a <a href=../../api/basic_json/number_integer_t/ ><code>number_integer_t</code></a> or <a href=../../api/basic_json/number_unsigned_t/ ><code>number_unsigned_t</code></a> wider than 64 bits (e.g., <code>__int128</code>) can hold values outside the range of the format: [-2^64, 2^64-1] for <abbr title="Concise Binary Object Representation">CBOR</abbr>, [-2^63, 2^64-1] for MessagePack, and the range of int64 (signed integers) or uint64 (unsigned integers) for <abbr title="Binary JSON">BSON</abbr>.</li> </ul> <p><a href=../../features/binary_formats/ubjson/ ><abbr title="Universal Binary JSON">UBJSON</abbr></a> and <a href=../../features/binary_formats/bjdata/ ><abbr title="Binary JData">BJData</abbr></a> never throw this exception, because they serialize integers beyond 64 bits as high-precision numbers.</p> <div class="admonition note"> <p class=admonition-title>Former occurrences</p> <p>The <abbr title="Universal Binary JSON">UBJSON</abbr> and <abbr title="Binary JSON">BSON</abbr> writers used to throw this exception as well:</p> <ul> <li>Since version 3.9.0, integer numbers beyond int64 are serialized as high-precision <abbr title="Universal Binary JSON">UBJSON</abbr> numbers.</li> <li>Since version 3.12.0, integer numbers beyond int64 are serialized as uint64 <abbr title="Binary JSON">BSON</abbr> numbers.</li> </ul> <p>Before version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, <abbr title="Concise Binary Object Representation">CBOR</abbr>, MessagePack, and <abbr title="Binary JSON">BSON</abbr> silently truncated integers wider than 64 bits, and <abbr title="Binary JData">BJData</abbr> truncated unsigned integers wider than 64 bits.</p> </div> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>integer number 18446744073709551615 cannot be represented by BON8 as it does not fit int64
|
||||
</code></pre></div></p> </div> <h3 id=jsonexceptionout_of_range407>json.exception.out_of_range.407<a class=headerlink href=#jsonexceptionout_of_range407 title="Permanent link">¶</a></h3> <p>An integer number cannot be represented by the binary format it is serialized to:</p> <ul> <li><a href=../../features/binary_formats/bon8/ ><abbr title="Binary Object Notation 8">BON8</abbr></a> only stores integers that fit into int64, so <a href=../../api/basic_json/to_bon8/ ><code>to_bon8</code></a> throws for unsigned integers greater than 9223372036854775807 and, with a number type wider than 64 bits, for any integer outside the int64 range.</li> <li><a href=../../features/binary_formats/cbor/ ><abbr title="Concise Binary Object Representation">CBOR</abbr></a>, <a href=../../features/binary_formats/messagepack/ >MessagePack</a>, and <a href=../../features/binary_formats/bson/ ><abbr title="Binary JSON">BSON</abbr></a> store integers in at most 64 bits. With the default number types, every integer fits, but a <a href=../../api/basic_json/number_integer_t/ ><code>number_integer_t</code></a> or <a href=../../api/basic_json/number_unsigned_t/ ><code>number_unsigned_t</code></a> wider than 64 bits (e.g., <code>__int128</code>) can hold values outside the range of the format: [-2^64, 2^64-1] for <abbr title="Concise Binary Object Representation">CBOR</abbr>, [-2^63, 2^64-1] for MessagePack, and the range of int64 (signed integers) or uint64 (unsigned integers) for <abbr title="Binary JSON">BSON</abbr>.</li> </ul> <p><a href=../../features/binary_formats/ubjson/ ><abbr title="Universal Binary JSON">UBJSON</abbr></a> and <a href=../../features/binary_formats/bjdata/ ><abbr title="Binary JData">BJData</abbr></a> never throw this exception, because they serialize integers beyond 64 bits as high-precision numbers.</p> <div class="admonition note"> <p class=admonition-title>Former occurrences</p> <p>The <abbr title="Universal Binary JSON">UBJSON</abbr> and <abbr title="Binary JSON">BSON</abbr> writers used to throw this exception as well:</p> <ul> <li>Since version 3.9.0, integer numbers beyond int64 are serialized as high-precision <abbr title="Universal Binary JSON">UBJSON</abbr> numbers.</li> <li>Since version 3.12.0, integer numbers beyond int64 are serialized as uint64 <abbr title="Binary JSON">BSON</abbr> numbers.</li> </ul> <p>Before version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, <abbr title="Concise Binary Object Representation">CBOR</abbr>, MessagePack, and <abbr title="Binary JSON">BSON</abbr> silently truncated integers wider than 64 bits, and <abbr title="Binary JData">BJData</abbr> truncated unsigned integers wider than 64 bits that were elements of ND-arrays with <code>"_ArrayType_": "uint64"</code>.</p> </div> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>integer number 18446744073709551615 cannot be represented by BON8 as it does not fit int64
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>integer number 1267650600228229401496703205376 cannot be represented by CBOR as it does not fit [-2^64, 2^64-1]
|
||||
</code></pre></div></p> </div> <h3 id=jsonexceptionout_of_range408>json.exception.out_of_range.408<a class=headerlink href=#jsonexceptionout_of_range408 title="Permanent link">¶</a></h3> <p>The size of an array or object in a <a href=../../features/binary_formats/ >binary format</a> exceeds the maximal capacity: the size following <code>#</code> for <a href=../../features/binary_formats/ubjson/ ><abbr title="Universal Binary JSON">UBJSON</abbr></a>/<a href=../../features/binary_formats/bjdata/ ><abbr title="Binary JData">BJData</abbr></a>, or the encoded length for <a href=../../features/binary_formats/cbor/ ><abbr title="Concise Binary Object Representation">CBOR</abbr></a>.</p> <p>The exception is also thrown for a <a href=../../features/binary_formats/ubjson/ ><abbr title="Universal Binary JSON">UBJSON</abbr></a> array of a type that is encoded by its marker alone (<code>Z</code>, <code>T</code> or <code>F</code>) whose declared count exceeds 1,048,576 (<code>1 << 20</code>). Such an array has no payload, so its count alone decides how much memory is allocated, and a handful of bytes would otherwise describe billions of values. <a href=../../api/basic_json/to_ubjson/ ><code>to_ubjson</code></a> writes longer arrays of these types without the size and type annotation, so any value it produces can still be read back.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>excessive array size: 8658170730974374167
|
||||
</code></pre></div></p> </div> <h3 id=jsonexceptionout_of_range408>json.exception.out_of_range.408<a class=headerlink href=#jsonexceptionout_of_range408 title="Permanent link">¶</a></h3> <p>The size of an array or object in a <a href=../../features/binary_formats/ >binary format</a> exceeds the maximal capacity: the size following <code>#</code> for <a href=../../features/binary_formats/ubjson/ ><abbr title="Universal Binary JSON">UBJSON</abbr></a>/<a href=../../features/binary_formats/bjdata/ ><abbr title="Binary JData">BJData</abbr></a>, or the encoded length for <a href=../../features/binary_formats/cbor/ ><abbr title="Concise Binary Object Representation">CBOR</abbr></a>.</p> <p>The exception is also thrown for a <a href=../../features/binary_formats/ubjson/ ><abbr title="Universal Binary JSON">UBJSON</abbr></a> array of a type that is encoded by its marker alone (<code>Z</code>, <code>T</code> or <code>F</code>) whose declared count exceeds 1,048,576 (<code>1 << 20</code>). Such an array has no payload, so its count alone decides how much memory is allocated, and a handful of bytes would otherwise describe billions of values. <a href=../../api/basic_json/to_ubjson/ ><code>to_ubjson</code></a> writes longer arrays of these types without the <code>$</code> type annotation (the <code>#</code> count and one marker per element are kept), so any value it produces can still be read back.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>excessive array size: 8658170730974374167
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive array size
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive map size
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.out_of_range.408] syntax error while parsing UBJSON size: excessive array size
|
||||
</code></pre></div></p> </div> <h3 id=jsonexceptionout_of_range409>json.exception.out_of_range.409<a class=headerlink href=#jsonexceptionout_of_range409 title="Permanent link">¶</a></h3> <p>Key identifiers to be serialized to <abbr title="Binary JSON">BSON</abbr> cannot contain code point U+0000, since the key is stored as zero-terminated c-string.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>BSON key cannot contain code point U+0000 (at byte 2)
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionout_of_range410>json.exception.out_of_range.410<a class=headerlink href=#jsonexceptionout_of_range410 title="Permanent link">¶</a></h3> <p>This exception is thrown when an undefined value is used with <a href=../../api/macros/nlohmann_json_serialize_enum_strict/ ><code>NLOHMANN_JSON_SERIALIZE_ENUM_STRICT</code></a>, or when an array index in a <abbr title="JavaScript Object Notation">JSON</abbr> pointer exceeds the range of <code>size_type</code> (e.g., on 32-bit platforms).</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>enum value out of range
|
||||
array index 18446744073709551616 exceeds size_type
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionout_of_range411>json.exception.out_of_range.411<a class=headerlink href=#jsonexceptionout_of_range411 title="Permanent link">¶</a></h3> <p>A <abbr title="JavaScript Object Notation">JSON</abbr> Patch <code>add</code> operation cannot be applied because the target location's parent is neither an object nor an array. Per <a href="https://datatracker.ietf.org/doc/html/rfc6902"><abbr title="Request for Comments">RFC</abbr> 6902</a>, an <code>add</code> target must reference a member of an existing object or an element of an existing array; a primitive value (string, number, boolean, etc.) cannot receive a new member or element.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>cannot add value: the JSON Patch 'add' target's parent is of type string, but must be an object or array
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Before that, this situation hit an internal assertion (aborting the program in debug builds) or was silently ignored when assertions were disabled.</p> </div> <h3 id=jsonexceptionout_of_range412>json.exception.out_of_range.412<a class=headerlink href=#jsonexceptionout_of_range412 title="Permanent link">¶</a></h3> <p><abbr title="Binary JSON">BSON</abbr> stores the length of documents, arrays, strings, and binary values in a signed 32-bit integer, and MessagePack stores the length of strings, binary values, arrays, and objects in at most an unsigned 32-bit integer. This exception is thrown when a value is too large to be described by such a length field.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>BSON length 2147483661 exceeds maximum of 2147483647
|
||||
array index 18446744073709551615 exceeds size_type
|
||||
</code></pre></div> <p>The second message is thrown, for instance, by <code class=highlight><span class=n>json</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=s>"[1, 2]"</span><span class=p>).</span><span class=n>at</span><span class=p>(</span><span class=s>"/18446744073709551615"</span><span class=n>_json_pointer</span><span class=p>)</span></code> on a 64-bit platform, where <code>size_type</code> is <code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>size_t</span></code> and this index is <code>SIZE_MAX</code>. An index that does not fit into 64 bits (such as <code>18446744073709551616</code>) is not reported as 410 but as <a href=#jsonexceptionout_of_range404><code>out_of_range.404</code></a>.</p> </div> <h3 id=jsonexceptionout_of_range411>json.exception.out_of_range.411<a class=headerlink href=#jsonexceptionout_of_range411 title="Permanent link">¶</a></h3> <p>A <abbr title="JavaScript Object Notation">JSON</abbr> Patch <code>add</code> operation cannot be applied because the target location's parent is neither an object nor an array. Per <a href="https://datatracker.ietf.org/doc/html/rfc6902"><abbr title="Request for Comments">RFC</abbr> 6902</a>, an <code>add</code> target must reference a member of an existing object or an element of an existing array; a primitive value (string, number, boolean, etc.) cannot receive a new member or element.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>cannot add value: the JSON Patch 'add' target's parent is of type string, but must be an object or array
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Before that, this situation hit an internal assertion (aborting the program in debug builds) or was silently ignored when assertions were disabled.</p> <p>A <code>null</code> parent is rejected as well. Before version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, an <code>add</code> into a <code>null</code> parent silently converted it to an object (e.g., <code>add</code> at <code>/foo/bar</code> turned <code>{"foo":null}</code> into <code>{"foo":{"bar":1}}</code>).</p> </div> <h3 id=jsonexceptionout_of_range412>json.exception.out_of_range.412<a class=headerlink href=#jsonexceptionout_of_range412 title="Permanent link">¶</a></h3> <p><abbr title="Binary JSON">BSON</abbr> stores the length of documents, arrays, strings, and binary values in a signed 32-bit integer, and MessagePack stores the length of strings, binary values, arrays, and objects in at most an unsigned 32-bit integer. This exception is thrown when a value is too large to be described by such a length field.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>BSON length 2147483661 exceeds maximum of 2147483647
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>MessagePack length 4294967296 exceeds maximum of 4294967295
|
||||
</code></pre></div></p> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Before that, the <abbr title="Binary JSON">BSON</abbr> length was silently truncated, and <a href=../../api/basic_json/to_bson/ ><code>to_bson</code></a> produced documents with negative length prefixes that <a href=../../api/basic_json/from_bson/ ><code>from_bson</code></a> rejected; <a href=../../api/basic_json/to_msgpack/ ><code>to_msgpack</code></a> wrote such a value without any length, producing output that could not be read back.</p> </div> <h3 id=jsonexceptionout_of_range413>json.exception.out_of_range.413<a class=headerlink href=#jsonexceptionout_of_range413 title="Permanent link">¶</a></h3> <p>A <abbr title="JavaScript Object Notation">JSON</abbr> Patch <code>remove</code> operation cannot be applied because the target location's parent is neither an object nor an array. Per <a href="https://datatracker.ietf.org/doc/html/rfc6902"><abbr title="Request for Comments">RFC</abbr> 6902</a>, a <code>remove</code> target must reference a member of an existing object or an element of an existing array; a primitive value (string, number, boolean, etc.) or <code>null</code> has no members or elements to remove.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>cannot remove value: the JSON Patch 'remove' target's parent is of type number, but must be an object or array
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Before that, this situation was silently ignored (the <code>remove</code> operation had no effect).</p> </div> <h3 id=jsonexceptionout_of_range414>json.exception.out_of_range.414<a class=headerlink href=#jsonexceptionout_of_range414 title="Permanent link">¶</a></h3> <p>A <abbr title="JavaScript Object Notation">JSON</abbr> Patch <code>move</code> operation's <code>"from"</code> location is a proper prefix of its <code>"path"</code> location. Per <a href="https://datatracker.ietf.org/doc/html/rfc6902"><abbr title="Request for Comments">RFC</abbr> 6902</a> (section 4.4), a location cannot be moved into one of its own children.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>cannot move value: 'from' path '/0' is a proper prefix of 'path' '/0/0'
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Before that, this situation could succeed with a corrupted result: for an array target, removing the "from" element before the "add" step shifted subsequent indices, so "path" silently re-resolved to a different element than intended.</p> </div> <h3 id=jsonexceptionout_of_range415>json.exception.out_of_range.415<a class=headerlink href=#jsonexceptionout_of_range415 title="Permanent link">¶</a></h3> <p>MessagePack's ext type and <abbr title="Binary JSON">BSON</abbr>'s binary subtype are each stored in a single byte. This exception is thrown when serializing a <a href=../../api/byte_container_with_subtype/ ><code>byte_container_with_subtype</code></a> whose subtype exceeds 255.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.out_of_range.415] subtype 70000 is too large for the MessagePack ext type (max 255)
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Before that, subtypes above 255 were silently truncated modulo 256 instead of raising an error.</p> </div> <h3 id=jsonexceptionout_of_range416>json.exception.out_of_range.416<a class=headerlink href=#jsonexceptionout_of_range416 title="Permanent link">¶</a></h3> <p><a href=../../api/basic_json_document/parse/ ><code>basic_json_document::parse()</code></a> and the other parsing functions of <a href=../../api/basic_json_document/ ><code>basic_json_document</code></a> index a value's position in the source text in 32 bits, so they do not support an input of 4294967280 bytes (4 GiB minus 16 bytes) or more. The same 32-bit limit applies to an <strong>editable</strong> document's own storage: <a href=../../api/basic_json_document/set/ ><code>set</code></a> and <a href=../../api/basic_json_document/push_back/ ><code>push_back</code></a> throw this exception once the strings and number tokens written by edits reach 4 GiB in total, or once more than 4294967295 arrays/objects have had an element set or appended to them. The same limit applies to an <a href=../../features/json_view/#images>image</a>: <a href=../../api/basic_json_document/save/ ><code>save()</code></a> throws it if the node count, the text, or the decoded strings it would write would individually reach 4 GiB.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>[json.exception.out_of_range.416] input of 4294967280 bytes or more is not supported by json_document
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Before that, this situation could succeed with a corrupted result: for an array target, removing the "from" element before the "add" step shifted subsequent indices, so "path" silently re-resolved to a different element than intended.</p> </div> <h3 id=jsonexceptionout_of_range415>json.exception.out_of_range.415<a class=headerlink href=#jsonexceptionout_of_range415 title="Permanent link">¶</a></h3> <p>MessagePack's ext type and <abbr title="Binary JSON">BSON</abbr>'s binary subtype are each stored in a single byte. This exception is thrown when serializing a <a href=../../api/byte_container_with_subtype/ ><code>byte_container_with_subtype</code></a> whose subtype exceeds 255, the largest value the library can store in that byte.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <div class=highlight><pre><span></span><code>[json.exception.out_of_range.415] subtype 70000 is too large for the MessagePack ext type (max 255)
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.out_of_range.415] subtype 70000 is too large for the BSON binary subtype (max 255)
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Before that, subtypes above 255 were silently truncated modulo 256 instead of raising an error.</p> </div> <h3 id=jsonexceptionout_of_range416>json.exception.out_of_range.416<a class=headerlink href=#jsonexceptionout_of_range416 title="Permanent link">¶</a></h3> <p><a href=../../api/basic_json_document/parse/ ><code>basic_json_document::parse()</code></a> and the other parsing functions of <a href=../../api/basic_json_document/ ><code>basic_json_document</code></a> index a value's position in the source text in 32 bits, so they do not support an input of 4294967280 bytes (4 <abbr title="Gibibyte (2^30 bytes)">GiB</abbr> minus 16 bytes) or more. The same 32-bit limit applies to an <strong>editable</strong> document's own storage: <a href=../../api/basic_json_document/set/ ><code>set</code></a> and <a href=../../api/basic_json_document/push_back/ ><code>push_back</code></a> throw this exception once the strings and number tokens written by edits reach 4 <abbr title="Gibibyte (2^30 bytes)">GiB</abbr> in total, or once more than 4294967295 arrays/objects have had an element set or appended to them. The same limit applies to an <a href=../../features/json_view/#images>image</a>: <a href=../../api/basic_json_document/save/ ><code>save()</code></a> throws it if the node count, the text, or the decoded strings it would write would individually reach 4 <abbr title="Gibibyte (2^30 bytes)">GiB</abbr>.</p> <div class="admonition failure"> <p class=admonition-title>Example messages</p> <p><div class=highlight><pre><span></span><code>[json.exception.out_of_range.416] input of 4294967280 bytes or more is not supported by json_document
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.out_of_range.416] edits of 4 GiB or more are not supported by json_document
|
||||
</code></pre></div> <div class=highlight><pre><span></span><code>[json.exception.out_of_range.416] images of 4 GiB or more are not supported by json_document
|
||||
</code></pre></div></p> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>, together with <a href=../../features/json_view/ ><code><nlohmann/json_view.hpp></code></a>. <a href=../../api/basic_json/parse/ ><code>basic_json::parse()</code></a> has no such limit.</p> </div> <h2 id=further-exceptions>Further exceptions<a class=headerlink href=#further-exceptions title="Permanent link">¶</a></h2> <p>This exception is thrown in case of errors that cannot be classified with the other exception types.</p> <p>Exceptions have ids 5xx.</p> <details class=example> <summary>Example: catch an <code>other_error</code> exception</summary> <p>The following code shows how an <code>other_error</code> exception can be caught.</p> <div class=highlight><pre><span></span><code><span class=cp>#include</span><span class=w> </span><span class=cpf><iostream></span>
|
||||
<span class=cp>#include</span><span class=w> </span><span class=cpf><nlohmann/json.hpp></span>
|
||||
|
||||
<span class=k>using</span><span class=w> </span><span class=n>json</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=n>nlohmann</span><span class=o>::</span><span class=n>json</span><span class=p>;</span>
|
||||
<span class=k>using</span><span class=w> </span><span class=k>namespace</span><span class=w> </span><span class=nn>nlohmann</span><span class=o>::</span><span class=nn>literals</span><span class=p>;</span>
|
||||
|
||||
<span class=kt>int</span><span class=w> </span><span class=nf>main</span><span class=p>()</span>
|
||||
@@ -302,11 +311,11 @@ array index 18446744073709551616 exceeds size_type
|
||||
<span class=w> </span><span class=p>{</span>
|
||||
<span class=w> </span><span class=c1>// output exception information</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>cout</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"message: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>what</span><span class=p>()</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span>
|
||||
<span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"exception id: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>id</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>endl</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=o><<</span><span class=w> </span><span class=s>"exception id: "</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=n>e</span><span class=p>.</span><span class=n>id</span><span class=w> </span><span class=o><<</span><span class=w> </span><span class=sc>'\n'</span><span class=p>;</span>
|
||||
<span class=w> </span><span class=p>}</span>
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Output:</p> <div class=highlight><pre><span></span><code>message: [json.exception.other_error.501] unsuccessful: {"op":"test","path":"/best_biscuit/name","value":"Choco Leibniz"}
|
||||
exception id: 501
|
||||
</code></pre></div> </details> <h3 id=jsonexceptionother_error501>json.exception.other_error.501<a class=headerlink href=#jsonexceptionother_error501 title="Permanent link">¶</a></h3> <p>A <abbr title="JavaScript Object Notation">JSON</abbr> Patch operation 'test' failed. The unsuccessful operation is also printed.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <p>Executing <code class=highlight><span class=p>{</span><span class=nt>"op"</span><span class=p>:</span><span class=s2>"test"</span><span class=p>,</span><span class=w> </span><span class=nt>"path"</span><span class=p>:</span><span class=s2>"/baz"</span><span class=p>,</span><span class=w> </span><span class=nt>"value"</span><span class=p>:</span><span class=s2>"bar"</span><span class=p>}</span></code> on <code class=highlight><span class=p>{</span><span class=nt>"baz"</span><span class=p>:</span><span class=w> </span><span class=s2>"qux"</span><span class=p>}</span></code>:</p> <div class=highlight><pre><span></span><code>[json.exception.other_error.501] unsuccessful: {"op":"test","path":"/baz","value":"bar"}
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionother_error502>json.exception.other_error.502<a class=headerlink href=#jsonexceptionother_error502 title="Permanent link">¶</a></h3> <p><a href=../../api/basic_json/to_ubjson/ ><code>to_ubjson</code></a> and <a href=../../api/basic_json/to_bjdata/ ><code>to_bjdata</code></a> were called with <code>use_type = true</code> but <code>use_size = false</code>. <abbr title="Universal Binary JSON">UBJSON</abbr> requires a size marker (<code>#</code>) after a type marker (<code>$</code>).</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.other_error.502] use_type requires use_size = true
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Before that, debug builds aborted on an assertion and release builds wrote a <code>$</code> marker without <code>#</code>, which <a href=../../api/basic_json/from_ubjson/ ><code>from_ubjson</code></a> then rejected.</p> </div> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 25, 2026 05:18:07 UTC">October 25, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 384 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.Line truncated
|
||||
</code></pre></div> </div> <h3 id=jsonexceptionother_error502>json.exception.other_error.502<a class=headerlink href=#jsonexceptionother_error502 title="Permanent link">¶</a></h3> <p><a href=../../api/basic_json/to_ubjson/ ><code>to_ubjson</code></a> and <a href=../../api/basic_json/to_bjdata/ ><code>to_bjdata</code></a> were called with <code>use_type = true</code> but <code>use_size = false</code>, and the value contains a non-empty array or object (or, with <abbr title="Binary JData">BJData</abbr> Draft 3, any binary value). <abbr title="Universal Binary JSON">UBJSON</abbr> requires a size marker (<code>#</code>) after a type marker (<code>$</code>); scalars and empty arrays or objects do not throw.</p> <div class="admonition failure"> <p class=admonition-title>Example message</p> <div class=highlight><pre><span></span><code>[json.exception.other_error.502] use_type requires use_size = true
|
||||
</code></pre></div> </div> <div class="admonition note"> <p class=admonition-title>Note</p> <p>This exception was added in version 3.13.0 <span class=unreleased-version title="Not part of a release yet; the latest release is 3.12.0.">unreleased</span>. Before that, debug builds aborted on an assertion and release builds wrote a <code>$</code> marker without <code>#</code>, which <a href=../../api/basic_json/from_ubjson/ ><code>from_ubjson</code></a> then rejected.</p> </div> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 26, 2026 19:13:30 UTC">October 26, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 384 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.Line truncated
|
||||
Reference in new issue
Block a user