简体   繁体   中英

when saving json formatted string on database, should i also save the type of value to convert it back later?

I'm using rapidjson on c++ to convert some numbers and strings to json format. then saving them on a column of mysql table as TEXT.

Later when i want to convert that TEXT to actual value:

  • how do i know the value type of the TEXT?
  • does json do that for me? convert to the actual type?
  • should i have to know the type in advance?

thanks for the upcoming helps.

how do i know the value type of the TEXT?

You can check if it is a string, float, integer, boolean, null or an array.

does json do that for me? convert to the actual type?

JSON is just a notation, it doesn't do anything. But your library which parses the values may do it. For example, strings will be wrapped in quotation marks. Or array will be wrapped in brackets. Intergers and floats are bare but valid numbers.

should i have to know the type in advance?

It depends on your application. When you're constructing a JSON, you probably know the type of each key, so when you are parsing that JSON, you can use that information.

Reading documentation of RapidJSON, it can parse and deduce the type of values. So, just use the library properly and it's enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM