/**
* Copyright 2012 cubiculus.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.coroptis.cubiculus.rest.mappers;
import java.util.ArrayList;
import com.coroptis.cubiculus.rest.model.AjProperty;
import com.coroptis.cubiculus.rest.model.AjPropertyValue;
import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;
import com.sdicons.json.mapper.helper.SimpleMapperHelper;
import com.sdicons.json.mapper.helper.impl.ObjectMapper;
import com.sdicons.json.model.JSONArray;
import com.sdicons.json.model.JSONObject;
import com.sdicons.json.model.JSONValue;
public class JsonMapperAjProperty implements SimpleMapperHelper {
@Override
public Class getHelpedClass() {
return AjProperty.class;
}
@Override
public Object toJava(JSONValue aValue, Class aRequestedClass) throws MapperException {
if (aValue.isObject()) {
final JSONObject jsonObject = (JSONObject) aValue;
final AjProperty out = new AjProperty();
out.setIdProperty(Helper.getIntRequired(jsonObject, "idProperty",
"AjProperty property idProperty was not found in json data"));
out.setName(Helper.getStringRequired(jsonObject, "name",
"AjProperty property name was not found in json data"));
out.setValues(new ArrayList<AjPropertyValue>());
JSONArray array = (JSONArray) jsonObject.get("values");
for (JSONValue jsonValue : array.getValue()) {
out.getValues().add(
(AjPropertyValue) JSONMapper.toJava(jsonValue, AjPropertyValue.class));
}
return out;
}
throw new MapperException("AjPropertyValue cannot map: " + aValue.getClass().getName());
}
@Override
public JSONValue toJSON(Object aPojo) throws MapperException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.toJSON(aPojo);
}
}