Skip to content

Commit e27395f

Browse files
committed
readme: avoiding type mismatch
1 parent 103ec55 commit e27395f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,22 @@ Any *Hjson::Value* of type *Hjson::Type::Double* will be represented by a string
272272

273273
The function *Hjson::Value::is_numeric()* returns true if the *Hjson::Value* is of type *Hjson::Type::Double* or *Hjson::Type::Int64*.
274274

275+
### Avoiding type mismatch errors
276+
277+
When *static_cast<>()* is used on an *Hjson::Value*, an *Hjson::type_mismatch* error is thrown if the *Hjson::Value* is not of a compatible type. For example, if you do `static_cast<const char*>(myVal)` then `myVal` must be of the type *Hjson::Type::String*, otherwise an *Hjson::type_mismatch* error is thrown.
278+
279+
So if you use static or implicit casting, errors can be thrown depending on the content of the input *Hjson* file. Maybe the *Hjson* file contains a collection of first and last names in quoteless lowercase strings and [Christopher Null](https://en.wikipedia.org/wiki/Christopher_Null) is one of the persons in the collection... The unquoted string `null` is stored in an *Hjson::Value* of type *Hjson::Type::Null* instead of *Hjson::Type::String* as the other names.
280+
281+
To avoid errors being thrown when casting, instead use these *Hjson::Value* member functions that will return the best possible representation of the value regardless of *Hjson::Type*:
282+
283+
```cpp
284+
double Value::to_double() const;
285+
std::int64_t Value::to_int64() const;
286+
std::string Value::to_string() const;
287+
```
288+
289+
For example, if `myVal` is of type *Hjson::Type::Null* then `myVal.to_string()` returns the string `"null"`.
290+
275291
### Operators
276292

277293
This table shows all operators defined for *Hjson::Value*, and the *Hjson::Type* they require. If an operator is used on an *Hjson::Value* of a type for which that operator is not valid, the exception *Hjson::type_mismatch* is thrown.

0 commit comments

Comments
 (0)