Skip to content

Unable to test key is<String>() #763

@sticilface

Description

@sticilface

I'm trying to use some templated functions to retrieve values from a JsonObject see below

template<typename T>
bool parse(const __FlashStringHelper * key, JsonObject & root, T & dest)
{
	if (root.containsKey(key) && root[key].is<T>()) {
		dest = root.get<T>(key);
		return true; 
	} else {
		return false;
	}
}

and i've discovered that you cannot test a Key for being a String. I know you can test for const char * instead but as in my first example it has to be templated so i cannot use const char * when passing String in...

here is a minimal sketch

#include <ArduinoJson.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); 
  DynamicJsonBuffer buffer;
  JsonObject & root = buffer.createObject();
  String myString = "hello"; 
  root["test"] = myString; 

  if (root["test"].is<String>()) {
    Serial.println("root is String"); 
  }
}

void loop() {
  // put your main code here, to run repeatedly:
}

I know String is not included in the list of types you can test for using is<T>(). is there a specific reason for this?

Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions