I have serialized ArrayCollection with Objects, everything is serialized into json with jms/serializer-bundle.
When I want deserialize my collection with keys (it's important):
deserialize($data, 'ArrayCollection<int, FooBundle\Entity\Item>', 'json');
I get array. But when I make new ArrayCollection with results:
new ArrayCollection(deserialize($data, 'ArrayCollection<int, FooBundle\Entity\Item>', 'json'));
Everything is fine and I get ArrayCollection with Objects and all keys are kept. Maybe someone knows why 1st method doesn't work?
Jms serializer has possibility to deserialize into ArrayCOllection f Objects section #Type here: http://jmsyst.com/libs/serializer/master/reference/annotations
Only solution that I found was creating separate class with items (ArrayCollection type) field. Here is jms-serializer definition for this class:
FooBundle\Entity\Cart:
exclusion_policy: ALL
properties:
items:
expose: true
type: ArrayCollection<int, FooBundle\Entity\CartItem>
Related
I am using FOSrestBundle in my Symfony2 project. I have a view created like this:
$view = $this
->view(array(
'form' => $this->formHandler->getForm()->createView(),
'translation' => $translation,
), Response::HTTP_OK)
->setTemplate('MyBundle:Translation.html.twig');
Where $translation is an object of my own bundle. The thing is when I call the $this->handleView($view), FosRestBundle use JMS serializer to serialize the data of my view (the form and the translation object) but my translation object have a lot of attributes useless in my case and the response is far too big for what I am trying to do.
I decide to use the group annotation to only retrieve useful attributes.
Here is the context with the view group:
$context = SerializationContext::create()->setGroups(array('view'));
$view->setSerializationContext($context);
And in my Translation model I can set the ExclusionPolicy to all and add usefull attributes to the view group. It is working but with this configuration (the group view in the serialization context) my form object (which is a Symfony\Component\Form\FormView) is serialized to {}
How can I use a group for my Translation model but still serialize my FormView object ?
If you're using annotations the JMS serializer has exclusion policies for each class, which you can see here.
I would suggest instead to default to exclude all and add the serializer groups annotation on only properties you want to expose. You can add multiple groups, so in this case your serializer context could have the groups "form" and "translationBasic", then add the "form" group to all properties on formView and "translationBasic" to just those you want on the Translation class.
I have a DAO like:
#Getter
#Setter
#DynamoDBTable(tableName="tableName")
public class DAO {
#DynamoDBHashKey
private String field1;
#DynamoDBIndexHashKey(globalSecondaryIndexName="index_name")
#DynamoDBRangeKey
private String field2;
}
Problem is when I am trying to use the DAO to make a load call using DynamoDBMapper with field1 as the hash key to obtain the item, it throws a DynamoDBException saying:
Null key found for public DAO.getField2()
but actually table has value corresponding to field2.
Question, is this because of Lombok annotation instead of the manual mutator code and or in general we use Lombok and DynamoDBAnnotations together?
Here is a little more of an explanation and a TL;DR
You are calling the load method, which is mapped to the GetItem call. The DynamoDBMapper is trying to map that request based on your annotations. Your class has the #DynamoDBRangeKey annotation, and the GetItem call needs the full primary key to get the item, which means that the mapper will build out the primary key for the object.
Since Lombok has already generated your code (before runtime), it is not affecting the annotations you have already placed. And also since your annotations are on the fields rather than applying them to the getters, the mapper it is calling the generated Lombok getter. When it tries to serialize into a request, however, that getter is returning null because you have only set the hashKey.
TL;DR: load() translates to GetItem API which requires both the hashKey and the rangeKey since both annotations are present on your class.
I've using a DataTransformer on my form to reverseTransform a decimal value send by the client into a Money/Money object.
This works well as expected, however when I return the data to the client using the FosRestBundle view layer, I'm not sure how I can use that transformer to transform it back into a decimal value for the client?
If you are using JMSSerializer with FOSRestBundle the serializer usually accesses the values of your object using reflection so the getters aren't touched.
You can however set the access type for the property or all of the properties in the object to the public method (getters/setters) using the access_type setting.
For Annotation you would use..
use JMS\Serializer\Annotation\AccessType;
class YourClass
{
/**
* #AccessType("public_method")
*/
private $money;
....
}
For more info see:
Annotations: http://jmsyst.com/libs/serializer/master/reference/annotations#accessor
XML: http://jmsyst.com/libs/serializer/master/reference/xml_reference
YAML: http://jmsyst.com/libs/serializer/master/reference/yml_reference
I have two classes, File and SearchResults. ShearchResults has an array of files. The relation between them is ManyToMany. This is how i mapped the relationship:
HasManyToMany<File>(x => x.Files).Table("refSearchResultsFiles").ParentKeyColumn("[SearchResult]").ChildKeyColumn("[File]");
When i try to save a SearchResult object, i get this exception:
Unable to cast object of type 'NHibernate.Collection.PersistentBag' to type 'TankusFileSharingClassLibrary.Entities.File[]'.
Why is this?
you are missing .AsArray(...) in the hasmanytomany
I ran into an error when trying to serialize a linq entitySet. To get around this i converted the entitySet to a list. The problem I have run into now is that it's child entity sets are not converting to a list and when I try to serialize the parent those are now throwing an error. Does anyone know of a way to convert a linq entitySet AND it's children to lists?
p.s. I'm new to linq so if any of this dosn't make sense let me know
Just project onto new types:
var q = from e in Entities
select new
{
Id = e.Id,
Name = e.Name,
Children = from c in e.Children
select new
{
Id = c.Id,
Name = c.Name,
// etc.
},
// etc.
};
var s = serializer.Serialize(q);
I am guessing you are trying to serialize to XML.
Either way, the problem stems from the circular references in LINQ entity objects. Lets say you have a main table Customers with a second table Orders. The Customers entity has a reference to all of the Orders this customer has (typically Customer.Orders). The Orders entity has a reference to the customer entity (typically Order.Customer). When the serializer tries to serialize this object, it recognizes the circular reference and throws an error.
In order to do what you want, you have to write your own serializer or convert the objects to something that can be directly serialized, like custom serialization objects which contain just the information you want to show up in the serialized version.