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

Empty Objects get serialized as "array()" #271

Closed
wjaspers opened this issue Feb 20, 2013 · 1 comment
Closed

Empty Objects get serialized as "array()" #271

wjaspers opened this issue Feb 20, 2013 · 1 comment

Comments

@wjaspers
Copy link

This is a problem for JSON serialization wherein a JSON schema is defined and validated against.

For example:

  1. I have an object with JMS Serializer annotations on it (in my case, a Doctrine entity).
  2. The Object has properties, but all which are annotated to be serialized are defaulted to "null".
  3. My application's controller serializes the object mentioned above using the JMS Serializer.
  4. My application's controller validates the response body against my schema.
  5. The application invalidates the response because the schema expects an object, but an empty array is provided to the validator.

example JSON schema:

{
   "name": "thingy",
   "type": "object",
   "properties" : {
        "name" : {
             "type" : "string",
             "maxlength" : 255,
        }
   }
}

example object:

use JMS\Serializer\Annotation as Serializer;

class Thingy {
  /**
   * @Serializer\Accessor(getter="getName")
   * @Serializer\SerializedName(value="name")
   * @Serializer\Expose
   */
  private $name;

  public function getName() {
     return $this->name;
  }
}

example serialization:

$thingy = new Thingy();
$result = $serializer->serialize($thingy, 'json');
var_dump($result);

result (JSON):
[]

expected result (JSON):
{}

@schmittjoh
Copy link
Owner

Closing this in favor of the issue on serializer lib.

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

No branches or pull requests

2 participants