Java tutorial
/* * Copyright (c) 2014 TIKINOU LLC * * 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.tikinou.schedulesdirect.core.jackson.deser; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import java.io.IOException; /** * @author Sebastien Astie */ public class BooleanYNDeserializer extends JsonDeserializer<Boolean> { @Override public Boolean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken t = jp.getCurrentToken(); if (t == JsonToken.VALUE_TRUE) { return Boolean.TRUE; } if (t == JsonToken.VALUE_FALSE) { return Boolean.FALSE; } if (t == JsonToken.VALUE_NULL) { return null; } if (t == JsonToken.VALUE_NUMBER_INT) { return (jp.getIntValue() != 0); } if (t == JsonToken.VALUE_STRING) { String text = jp.getText().trim(); if ("true".equals(text)) { return Boolean.TRUE; } if ("false".equals(text) || text.length() == 0) { return Boolean.FALSE; } if ("N".equalsIgnoreCase(text) || text.length() == 0) { return Boolean.FALSE; } if ("Y".equalsIgnoreCase(text)) { return Boolean.TRUE; } throw ctxt.weirdStringException(text, Boolean.class, "only \"true\" or \"false\" recognized"); } // Otherwise, no can do: throw ctxt.mappingException(Boolean.class); } }