Ask a questionAsk a question
 

AnswerEntities and JSON serialization

  • Sunday, November 08, 2009 12:00 AMBrian M. D. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi guys,

    I'm trying to figure out the best way to use entities with JSON serialization. I have a WCF web service that I wan't to be able to accept an entity as a parameter. However becasue the entities have the DataContact attributes IsReference property set to true the entity cannot be serialized into JSON. Is there anyway to make an entity JSON serializable?

    My current solution to the problem is creating other classes that represent the entities, and then transferring the property values over to the actual entities.

    For example, I have a Claimant entity and an SClaimant class with the same properties as the Claimant entity. Here is the service method that implements this:

            public long AddClaimant(SClaimant claimant)
            {
                Claimant claimantEntity = new Claimant();
                claimantEntity.FirstName = claimant.FirstName;
                claimantEntity.MiddleInitial = claimant.MiddleInitial;
                claimantEntity.LastName = claimant.LastName;
                claimantEntity.Address = claimant.Address;
                claimantEntity.Province = int.Parse(claimant.Province);
                claimantEntity.City = claimant.City;
                claimantEntity.PostalCode = claimant.PostalCode;
                claimantEntity.HomePhone = claimant.HomePhone;
                claimantEntity.Mobile = claimant.Mobile;
                claimantEntity.Email = claimant.Email;
                
                Claimant addedClaimant =
                    ClaimantManager.AddClaimant(claimantEntity);
    
                return addedClaimant.Id;
            }
    
    This solution is working but isn't ideal. Is it possible to make an entity JSON serializable? And if not is it possible to have an entity implement a custom interface so that the entity object and the serializable object both inherit the same interface?

    If anyone has any insight into this I would greatly appreciate the help.

    Thanks in advance,

    Brian

Answers

All Replies