Toggle navigation
Toggle navigation
This project
Loading...
Sign in
홍길동
/
onos
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Thomas Vachuska
2014-10-23 14:19:46 -0700
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
d404c5177bc1cf532330fe2adfa5f2681f8653b9
d404c517
1 parent
868def0f
Added initial sketch of JSON codec abstraction and related codec tracking service.
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
254 additions
and
0 deletions
core/api/src/main/java/org/onlab/onos/codec/CodecService.java
core/api/src/main/java/org/onlab/onos/codec/JsonCodec.java
core/api/src/main/java/org/onlab/onos/codec/package-info.java
core/api/src/test/java/org/onlab/onos/codec/JsonCodecTest.java
core/api/src/main/java/org/onlab/onos/codec/CodecService.java
0 → 100644
View file @
d404c51
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
org
.
onlab
.
onos
.
codec
;
import
java.util.Set
;
/**
* Service for registering and retrieving JSON codecs for various entities.
*/
public
interface
CodecService
{
/**
* Returns the set of classes with currently registered codecs.
*
* @return set of entity classes
*/
Set
<
Class
<?>>
getCodecs
();
/**
* Returns the JSON codec for the specified entity class.
*
* @param entityClass entity class
* @return JSON codec; null if no codec available for the class
*/
JsonCodec
getCodec
(
Class
<?>
entityClass
);
/**
* Registers the specified JSON codec for the given entity class.
*
* @param entityClass entity class
* @param codec JSON codec
*/
void
registerCodec
(
Class
<?>
entityClass
,
JsonCodec
codec
);
/**
* Unregisters the JSON codec for the specified entity class.
*
* @param entityClass entity class
*/
void
unregisterCodec
(
Class
<?>
entityClass
);
}
core/api/src/main/java/org/onlab/onos/codec/JsonCodec.java
0 → 100644
View file @
d404c51
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
org
.
onlab
.
onos
.
codec
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* Abstraction of a codec capable for encoding/decoding arbitrary objects to/from JSON.
*/
public
abstract
class
JsonCodec
<
T
>
{
/**
* Encodes the specified entity into JSON.
*
* @param entity entity to encode
* @param mapper object mapper
* @return JSON node
* @throws java.lang.UnsupportedOperationException if the codec does not
* support encode operations
*/
public
abstract
ObjectNode
encode
(
T
entity
,
ObjectMapper
mapper
);
/**
* Decodes the specified entity from JSON.
*
* @param json JSON to decode
* @return decoded entity
* @throws java.lang.UnsupportedOperationException if the codec does not
* support decode operations
*/
public
abstract
T
decode
(
ObjectNode
json
);
/**
* Encodes the collection of the specified entities.
*
* @param entities collection of entities to encode
* @param mapper object mapper
* @return JSON array
* @throws java.lang.UnsupportedOperationException if the codec does not
* support encode operations
*/
public
ArrayNode
encode
(
Iterable
<
T
>
entities
,
ObjectMapper
mapper
)
{
ArrayNode
result
=
mapper
.
createArrayNode
();
for
(
T
entity
:
entities
)
{
result
.
add
(
encode
(
entity
,
mapper
));
}
return
result
;
}
/**
* Decodes the specified JSON array into a collection of entities.
*
* @param json JSON array to decode
* @return collection of decoded entities
* @throws java.lang.UnsupportedOperationException if the codec does not
* support decode operations
*/
public
List
<
T
>
decode
(
ArrayNode
json
)
{
List
<
T
>
result
=
new
ArrayList
<>();
for
(
JsonNode
node
:
json
)
{
result
.
add
(
decode
((
ObjectNode
)
node
));
}
return
result
;
}
}
core/api/src/main/java/org/onlab/onos/codec/package-info.java
0 → 100644
View file @
d404c51
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
/**
* Base JSON codec abstraction and a service for tracking various JSON codecs.
*/
package
org
.
onlab
.
onos
.
codec
;
\ No newline at end of file
core/api/src/test/java/org/onlab/onos/codec/JsonCodecTest.java
0 → 100644
View file @
d404c51
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
org
.
onlab
.
onos
.
codec
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
com.google.common.collect.ImmutableList
;
import
org.junit.Test
;
import
java.util.List
;
import
java.util.Objects
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
/**
* Test of the base JSON codec abstraction.
*/
public
class
JsonCodecTest
{
private
static
class
Foo
{
final
String
name
;
Foo
(
String
name
)
{
this
.
name
=
name
;
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
name
);
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
{
return
true
;
}
if
(
obj
==
null
||
getClass
()
!=
obj
.
getClass
())
{
return
false
;
}
final
Foo
other
=
(
Foo
)
obj
;
return
Objects
.
equals
(
this
.
name
,
other
.
name
);
}
}
private
static
class
FooCodec
extends
JsonCodec
<
Foo
>
{
@Override
public
ObjectNode
encode
(
Foo
entity
,
ObjectMapper
mapper
)
{
return
mapper
.
createObjectNode
().
put
(
"name"
,
entity
.
name
);
}
@Override
public
Foo
decode
(
ObjectNode
json
)
{
return
new
Foo
(
json
.
get
(
"name"
).
asText
());
}
}
@Test
public
void
encode
()
{
Foo
f1
=
new
Foo
(
"foo"
);
Foo
f2
=
new
Foo
(
"bar"
);
FooCodec
codec
=
new
FooCodec
();
ImmutableList
<
Foo
>
entities
=
ImmutableList
.
of
(
f1
,
f2
);
ArrayNode
json
=
codec
.
encode
(
entities
,
new
ObjectMapper
());
List
<
Foo
>
foos
=
codec
.
decode
(
json
);
assertEquals
(
"incorrect encode/decode"
,
entities
,
foos
);
}
}
\ No newline at end of file
Please
register
or
login
to post a comment