-
-
Notifications
You must be signed in to change notification settings - Fork 585
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
[Question] Serialization of primitive types #853
Comments
you cant define hadlers for native types. you have to Example:
properties:
active:
type: MY_boolean and define a handler for |
@goetas I've tried it, but it throws an error ReflectionException: Class custom_boolean does not exist in /.../jms/metadata/src/Metadata/MetadataFactory.php on line 167 |
class BooleanToStringHandler implements SubscribingHandlerInterface
{
public static function getSubscribingMethods()
{
return [
[
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
'format' => 'json',
'type' => 'custom_boolean',
'method' => 'serializeBooleanToString',
],
];
}
public function serializeBooleanToString(JsonSerializationVisitor $visitor, $value, array $type, Context $context)
{
// not reached
}
} is your handler defined as this? attention to |
Yes, it is. |
I managed to fix the problem, but I guess it is a bug. The behavior of my system which throws the exception is basically what is described below. $string = '{property:false}';
$object = $serializer->deserialize($string, Object::class, 'json');
$object->property = true;
$serializer->serialize($api, 'json'); I fixed the problem adding the deserialization to the handler. public static function getSubscribingMethods()
{
return [
[
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
'format' => 'json',
'type' => 'custom_boolean',
'method' => 'serializeBooleanToString',
],
[
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
'format' => 'json',
'type' => 'custom_boolean',
'method' => 'deserializeBooleanToString',
],
];
}
public function serializeBooleanToString(JsonSerializationVisitor $visitor, $value, array $type, Context $context)
{
return (true === $value)
? 'true'
: 'false'
;
}
public function deserializeBooleanToString(JsonDeserializationVisitor $visitor, $value, array $type, Context $context)
{
return $visitor->visitBoolean($value, $type, $context);
} |
@goetas you can check it in https://github.com/jvahldick/jms-error-issue-853 Changing it to the code below, it works. public static function getSubscribingMethods()
{
return [
[
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
'format' => 'json',
'type' => 'custom_boolean',
'method' => 'serializeBooleanToString',
],
[
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
'format' => 'json',
'type' => 'custom_boolean',
'method' => 'deserializeBooleanToString',
],
];
}
public function serializeBooleanToString(JsonSerializationVisitor $visitor, $value, array $type, Context $context)
{
return (true === $value)
? 'true'
: 'false'
;
}
public function deserializeBooleanToString(JsonDeserializationVisitor $visitor, $value, array $type, Context $context)
{
return $visitor->visitBoolean($value, $type, $context);
} |
in your repo, you are deserializing a custom type but you have defined only the serialization handler (not the de-serialization). that's the reason of the error you are getting |
I've a similar case described in the issue #631
I am trying to convert a boolean into a string, but it never reaches the defined method.
I saw these issues (#631, #610), but I couldn't really make it work.
I found the solution adding a SerializationVisitor, but I don't think it is the ideal solution for that.
Am I doing something wrong here?
The text was updated successfully, but these errors were encountered: