Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single char converted to ASCII code instead of string #413

Closed
sulliwane opened this issue Jan 2, 2017 · 4 comments
Closed

Single char converted to ASCII code instead of string #413

sulliwane opened this issue Jan 2, 2017 · 4 comments

Comments

@sulliwane
Copy link

Hello,

I'm getting two types of data in a api callback function:

typedef char type1;
typedef char type2[16];

and here is my function:

void spi::OnCallback(Message *message) {
  json data = json({});
  
  data["type1"] = message->type1;  // message.type1 === 'A'
  data["type2"] = message->type2; // message.type2 === 'a string'

  DEBUG(data.dump());
// data.type1 === 60
// data.type2 === 'a string'
};

Type2 is recognized as a string by the json lib, but it seems type1 being a single char is converted to the equivalent ASCII number. I'm a correct? So right now I handle this by explicitly converting the single char to a string like this:

data["type1"] = std::string(1, message->type1);

But this is not really convenient. Any idea on how to better handle this single char case, so that it's implicitly converted to a string by the library?

Many thanks for your help!

@sulliwane sulliwane changed the title Single char is stored as ASCII code Single char converted to ASCII code instead of string Jan 2, 2017
@nlohmann
Copy link
Owner

nlohmann commented Jan 2, 2017

I understand the issue, but I fear that implicitly converting a single character to a string could be surprising, because std::string has no such implicit conversion. I tried to make basic_json behave like a std::string when storing JSON strings.

For your convenience, you may implement a function like

json from_char(char c)
{
    return std::string(1, c);
}

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Jan 2, 2017
@jaredgrubb
Copy link
Contributor

char is weird and the STL treats the three types [char/signed char/unsigned char] inconsistently, in my opinion. For example, "std::cout << ((uint8_t)65)" will print "A", but "std::cout << ((uint16_t)65)" prints a number.

In my ideal world, 'char' would represent a character (not a number!) and 'signed char'/'unsigned char' (aka int8_t, uint8_t) would represent numeric values. There are three distinct types, but the STL gives them no semantic difference.

For purity, adding "basic_json(char) = delete" removes confusion because every implicit conversion doesn't make sense (either as a character or a number); note that uint8_t and int8_t still convert as a number.

But, in practice, I do not think it's worth the breaking change.

@sulliwane
Copy link
Author

I see, so I will continue with manual conversion to a string.

Many thanks both of you for your answers, very helpful :)

@nlohmann nlohmann removed the state: please discuss please discuss the issue or vote for your favorite option label Jan 3, 2017
@nlohmann
Copy link
Owner

nlohmann commented Jan 3, 2017

I also agree that deleting the basic_json(char) constructor is not worth it.

Thanks for the discussion! I shall add a not to the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants