Var question


#1

Hi.

I’m parsing a JSON file. It’s probably a stupid question but still:

var jsonString= JSON::parse(jsonFile);
var categories = jsonString[(Identifier)"categories"];

When I’m not using the Identifier casting I get the following error:

error C2666: ‘juce::var::operator []’ : 2 overloads have similar conversions
juce_amalgamated.h(8640): could be 'juce::var juce::var::operator [](const juce::Identifier &) const’
or 'built-in C++ operator[(int, const char [9])'
while trying to match the argument list ‘(juce::var, const char [9])’

Is there a better way to prevent the ambiguity?

Thanks


#2

I guess I could add an operator[] that takes a const char*, and that’d probably make it easier in cases like this.

A couple of tips though:

  • You should write Identifier (“xyz”) instead of (Identifier) “xyz”, because you’re invoking a constructor, not casting a const char*.
  • It’s much more efficient to pre-prepare some static const Identifier objects and use them instead of using hard-coded string literals in the operator call. Better coding practice too, to avoid magic numbers (well, magic strings in this case).

#3

Got it, Thanks Jules.