BUG-7983: unify JSONCodec and XmlCodec methods
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / codec / TypeAwareCodec.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.util.codec;
9
10 import com.google.common.annotations.Beta;
11
12 /**
13   * A codec, which knows what the native representation for a particular data type is. It knows how to convert a native
14   * value to and from string representation based on some additional input or output context.
15   *
16   * @author Robert Varga
17   *
18   * @param <T> Data value type
19   * @param <I> Input context type
20   * @param <O> Output context type
21   */
22 @Beta
23 public interface TypeAwareCodec<T, I, O> {
24     /**
25      * Return the data type class.
26      *
27      * @return Data type class
28      */
29     Class<T> getDataType();
30
31     /**
32      * Parse a String representation into its native format.
33      *
34      * @param ctx Parse context
35      * @param str String representation
36      * @return Value in native format
37      * @throws IllegalArgumentException if the value does not parse or pass type validation
38      */
39     T parseValue(I ctx, String str);
40
41     /**
42      * Serialize specified value with specified JsonWriter.
43      *
44      * @param ctx Write context
45      * @param value Value in native format
46      * @throws Exception if the write fails
47      */
48     void writeValue(O ctx, T value) throws Exception;
49 }