eu.mondo.driver.mongo.util.MStatementDeserializer.java Source code

Java tutorial

Introduction

Here is the source code for eu.mondo.driver.mongo.util.MStatementDeserializer.java

Source

/*******************************************************************************
 * Copyright (c) 2010-2014, Daniel Stein, Istvan Rath and Daniel Varro
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Daniel Stein - implementation for Mongo
 *******************************************************************************/

package eu.mondo.driver.mongo.util;

import java.io.IOException;
import java.math.BigInteger;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * @author Dniel Stein
 *
 */
public class MStatementDeserializer extends JsonDeserializer<MStatement> {

    @Override
    public MStatement deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {

        String objectString = null;
        String predicateString = null;
        String subjectString = null;

        BigInteger subjectBI;
        BigInteger predicateBI;
        BigInteger objectBI;

        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = mapper.readTree(jp);

        subjectString = root.path("subject").textValue();
        predicateString = root.path("predicate").textValue();
        objectString = root.path("object").textValue();

        String subjectBIS = root.path("subjectBI").textValue();
        String predicateBIS = root.path("predicateBI").textValue();
        String objectBIS = root.path("objectBI").textValue();
        subjectBI = "".equals(subjectBIS) || subjectBIS == null ? new BigInteger("0", 10)
                : new BigInteger(subjectBIS, 16);
        predicateBI = "".equals(predicateBIS) || predicateBIS == null ? new BigInteger("0", 10)
                : new BigInteger(predicateBIS, 16);
        objectBI = "".equals(objectBIS) || objectBIS == null ? new BigInteger("0", 10)
                : new BigInteger(objectBIS, 16);

        //        URI subject = new URIImpl(subjectString);
        //        URI predicate = new URIImpl(predicateString);
        //
        //        Value object;
        //        try {
        //            object = new URIImpl(objectString);
        //        } catch (Exception e) {
        //            object = ValueFactoryImpl.getInstance().createLiteral(objectString);
        //        }

        jp.close();

        MStatement statement = new MStatement();
        statement.setSubject(subjectString);
        statement.setPredicate(predicateString);
        statement.setObject(objectString);
        statement.setSubjectBI(subjectBI);
        statement.setPredicateBI(predicateBI);
        statement.setObjectBI(objectBI);

        return statement;
    }
}