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

Garabage data at first name in JsonObject,for returned JsonObject from class member function #785

Closed
pulkit-jain opened this issue Jul 20, 2018 · 4 comments
Labels
question v5 ArduinoJson 5

Comments

@pulkit-jain
Copy link

Hi all

JsonObject& GetJsonRoot() {
File lConfigFile = SPIFFS.open(CONFIG_FILE_PATH, "r");
DynamicJsonBuffer lJsonBuffer;
JsonObject* lJsonRoot = NULL;
if (!lConfigFile) {
Serial.println("Failed to open config file");
return *lJsonRoot;
}

size_t lFileSize = lConfigFile.size();

std::unique_ptr<char[]> lFileData(new char[lFileSize]);
lConfigFile.readBytes(lFileData.get(), lFileSize);

lConfigFile.close();

 lJsonRoot = &lJsonBuffer.parseObject(lFileData.get());

if (!lJsonRoot->success()) {
	Serial.println("Failed to parse config file");
	return *lJsonRoot;
}
lJsonRoot->printTo(Serial);
return *lJsonRoot;

}

Actually when i print lJsonRoot just at the end of GetJsonRoot,it is giving correct data.
But when i print the in the caller method it is giving garbage data for only first name of data.

Correct Data:-{"serverName":"api.example.com","accessToken":"128du9as8du12eoue8da98h123ueh9h98"}
Incorrect Data:- {"⸮�":"api.example.com","accessToken":"128du9as8du12eoue8da98h123ueh9h98"}

Thanks in advance for the help.

Kind Regards
Pulkit Jain

@pulkit-jain
Copy link
Author

I am adding code again in clear format.

JsonObject& GetJsonRoot() {
	File lConfigFile = SPIFFS.open(CONFIG_FILE_PATH, "r");
	DynamicJsonBuffer lJsonBuffer;
	JsonObject* lJsonRoot = NULL;
	if (!lConfigFile) {
		Serial.println("Failed to open config file");
		return *lJsonRoot;
	}

	size_t lFileSize = lConfigFile.size();

	std::unique_ptr<char[]> lFileData(new char[lFileSize]);
	lConfigFile.readBytes(lFileData.get(), lFileSize);

	lConfigFile.close();

	 lJsonRoot = &lJsonBuffer.parseObject(lFileData.get());

	if (!lJsonRoot->success()) {
		Serial.println("Failed to parse config file");
		return *lJsonRoot;
	}
	lJsonRoot->printTo(Serial);
	return *lJsonRoot;
}

@bblanchon
Copy link
Owner

Hi @pulkit-jain,

See:

Regards,
Benoit

@pulkit-jain
Copy link
Author

thanks Benoit!
I am able to resolve my problem by your input.

@bblanchon
Copy link
Owner

You're welcome @pulkit-jain.
Thank you for using ArduinoJson and good luck with your projects.

Repository owner locked and limited conversation to collaborators Nov 20, 2018
@bblanchon bblanchon added the v5 ArduinoJson 5 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question v5 ArduinoJson 5
Projects
None yet
Development

No branches or pull requests

2 participants