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

Allow set enum as json value #4363

Merged
merged 5 commits into from
Feb 1, 2024
Merged

Conversation

bas524
Copy link
Contributor

@bas524 bas524 commented Dec 21, 2023

fix issue #4341
add testEnum

        enum SAMPLE_ENUM
	{
		SE_VALUE = 42
	};
	
	Poco::JSON::Object::Ptr obj = new Poco::JSON::Object();
	obj->set("simple_enum", SE_VALUE);
	Poco::Dynamic::Var var(obj);
	std::string expected = "{\"simple_enum\":42}";
	std::string result = var.convert<std::string>();
	assertEquals(expected, result);
		
	SAMPLE_ENUM se = obj->get("simple_enum").extract<SAMPLE_ENUM>();
	assertTrue(se == SE_VALUE);

Canges are simple, I've added branch for every default method VarHolderImpl

     void convert(Int8& val) const
	{
		if constexpr (std::is_enum<T>::value)
		{
			convertToSigned(std::underlying_type_t<T>(_val), val);
		}
		else
		{
			VarHolder::convert(val);
		}
	}   

add testEnum
if you set enum as json value then it will be cast to int
Copy link
Contributor

@matejk matejk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is very good. It needs some more work to be complete.

Foundation/include/Poco/Dynamic/Var.h Outdated Show resolved Hide resolved
JSON/testsuite/src/JSONTest.cpp Show resolved Hide resolved
JSON/testsuite/src/JSONTest.cpp Outdated Show resolved Hide resolved
Copy link
Member

@aleks-f aleks-f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add Var tests, extract() must work

Foundation/include/Poco/Dynamic/VarHolder.h Outdated Show resolved Hide resolved
add testEnumType to the VarTest.cpp, extract() works
@bas524
Copy link
Contributor Author

bas524 commented Jan 7, 2024

please add Var tests, extract() must work

Ok, I've done

@aleks-f aleks-f changed the base branch from devel to 4341-json-enum February 1, 2024 11:46
@aleks-f aleks-f merged commit 7f8fe19 into pocoproject:4341-json-enum Feb 1, 2024
3 checks passed
@aleks-f
Copy link
Member

aleks-f commented Feb 1, 2024

merged into poco issue branch to trigger CI

@aleks-f
Copy link
Member

aleks-f commented Feb 1, 2024

#4434

matejk pushed a commit that referenced this pull request Mar 26, 2024
* fix issue #4341
add testEnum
if you set enum as json value then it will be cast to int

* add behavour for enum into VarHolderImpl
add tests for enum classes with differnent underline integral types

* replace static_cast with convertTo* methods, they can chaeck types and
limits

* fix missing types for convert methods

* fix code style
add testEnumType to the VarTest.cpp, extract() works
aleks-f added a commit that referenced this pull request Jul 29, 2024
* Allow set enum as json value (#4363)

* fix issue #4341
add testEnum
if you set enum as json value then it will be cast to int

* add behavour for enum into VarHolderImpl
add tests for enum classes with differnent underline integral types

* replace static_cast with convertTo* methods, they can chaeck types and
limits

* fix missing types for convert methods

* fix code style
add testEnumType to the VarTest.cpp, extract() works

* enh(VarHolder): replace ::value and ::type with shortcuts

---------

Co-authored-by: Alexander B <ale.bychuk@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants