EzDevInfo.com

ObjectMapper

Simple JSON Object mapping written in Swift

Alamofire, ObjectMapper, Realm: Delete orphaned objects

I use Alamofire, ObjectMapper and Realm and save my fetched and mapped objets in realm. Is it possible to delete orphaned objects automatically?

e.g. I fetch for a list of contacts. Now one contacts has been removed from the response and should be deleteted from Realm automatically. Is this something objectmapper can do?


Source: (StackOverflow)

Alamofire, Objectmapper, Realm: Nested Objects

I'm using Alamofire, Objectmapper, Realm and everything is working beside one thing: I can't map nested objects.

class Voting: Object, Mappable {

    dynamic var votingID: String = ""
    dynamic var question: String = ""
    var votingOptions = List<VotingOption>()

    required convenience init?(_ map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        votingID <- map["id"]
        question <- map["question"]
        votingOptions <- map["votingOptions"]
    }

    override class func primaryKey() -> String {
        return "votingID"
    }   
}

class VotingOption: Object, Mappable{

    dynamic var optionID: String = ""
    dynamic var text: String = ""


    required convenience init?(_ map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        optionID <- map["id"]
        text <- map["optionText"]
    }

    override class func primaryKey() -> String {
        return "optionID"
    }   
}

The JSON that I'm trying to map is:

{
    "Voting": [
        {
            "question": "Which option do yo prefer?",
            "id": "7f073efd-6f3d-43f2-9fe4-5cad683b77a2",
            "votingOptions": [
                {
                    "optionText": "Option 3",
                    "id": "3bc0a618-8791-4862-a7fd-5f2df464697d"
                },
                {
                    "optionText": "Option 1",
                    "id": "84c6a830-814b-40c8-a252-c074be5d689a"
                },
                {
                    "optionText": "Option 2",
                    "id": "8872ef6f-fc70-445a-802e-d39944006467"
                }
            ]
        }
    ]
}

The mapping funktion in VotingOption never gets called.


Source: (StackOverflow)

Advertisements

How to ignore attributes while serializing a class by ObjectMapper

I have a class with lots of attributes which are required for server side logic, but a few of those are required for UI. Now when I am creating a json from the class, all the attributes are written to json. I want to ignore some values only when it is converted to json. I Tried with @JsonIgnore. But it is not working.

My Class Is

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Student {

    @JsonProperty("id")
    protected Integer id;

    @JsonProperty("name")
    protected String name;

    /**
     * This field I want to ignore in json.
     * Thus used @JsonIgnore in its getter
     */
    @JsonProperty("securityCode")
    protected String securityCode;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @JsonIgnore
    public String getSecurityCode() {
        return securityCode;
    }

    public void setSecurityCode(String securityCode) {
        this.securityCode = securityCode;
    }
}

And I am writing this using

public static StringBuilder convertToJson(Object value){
        StringBuilder stringValue = new StringBuilder();
        ObjectMapper mapper = new ObjectMapper();
        try {
            stringValue.append(mapper.writeValueAsString(value));
        } catch (JsonProcessingException e) {
            logger.error("Error while converting to json>>",e);
        }
        return stringValue;
    }

My Expected json should contain only :

id:1
name:abc

but what I am getting is
id:1
name:abc
securityCode:_gshb_90880..some_value.

What is wrong here, please help


Source: (StackOverflow)