Skip to content

jmSfernandes/NGSIBaseModel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NGSIBaseModel

Maven Central

This is a multi-purpose parser for the NGSI10 standard. The NGSI10 standard is a the default standard used by ORION the main Componnent of the FIWARE project.

This parser is able to simply convert the Json objects received in the NGSI format (both standard and keyValues) to Java Classes. And also convert Java classes to the equivalent and compliant NGSI10 entities.

The Classes should extend the NGSIBaseModel. And fullfill the following constraints:

  • The class name should be the same as the entity type which we are parsing;

  • the fields of the class must be public, otherwise a reflection error will be raised due to the classes be in different packages;

  • You must use the JsonObject from the Gson library. You can easly convert from and to other Json libraries by calling the method toString() and parsing the string.

  • The attributes of the class should be named the same as the entity attributes (ignoring case);

  • If the class is to be sent to an orion instance the id field should be defined, otherwise the id and type attributes will not be defined by the base model (You can still defined them manually after retriving the json);

  • The class can have list field but they should either be List<String> or a List of another class that also extends the com.github.jmSfernandes.NGSIBaseModel

  • If you want to parse date strings directly the class field should be of type date. The model only supports the format "yyyy-MM-dd'T'HH:mm:ss'Z'"(ISO8601). For other formats create String fields and parse them accordingly after the model creates the objects.

  • If you have a field that supports any of the forbidden characters of ORION use the annotation @NGSIEncode. The parser will perform urlEncoding and decoding on fields marked with that annotation;

  • It is also possible to ignore field that are only client specific, with the annotation @NGSIIgnore. Those fields will not be added to the NGSI json Object and will not be parsed from the NGSI objects as well.

Example of a Class:

    
public class Car extends NGSIBaseModel{
    public String id;
    public String model;
    public String color;
    
    //this field will be converted into a JsonArray 
    public List<String> variations;
    
    //this field will be ignored
    @NGSIIgnore
    public String ignoreMe;
    
    //this field will be parsed 
    @NGSIEncoded
    public String encodeMe;

    //this field will be parsed from and to the format "yyyy-MM-dd'T'HH:mm:ss'Z'"
    public Date timestamp;
    ...
    
    }

From NGSI:

To convert json to a compliant class is just necessary to call the method fromNgsi()

    JsonObject json= new JsonObject(jsonString);
    Car car= new Car().fromNGSI(json);

To NGSI:

To convert json to a compliant class is just necessary to call the method fromNgsi()

     Car test = new Car();
     test.setId("car_1");
     test.setColor("red");
     test.setModel("corvette");

     test.setEncodeMe("This car is a simple car==!\"(with a red hood)");
     test.setIgnoreMe("I was ignored!");

     DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
     Date timestamp = df.parse("2020-10-07T09:50:00Z");
     test.setTimestamp(timestamp);
     
     List<String> variations = new ArrayList<String>();
     variations.add("Street");
     variations.add("Lounge");
     variations.add("Easy");
     variations.add("SportsWagon");
     test.setVariations(variations);
     
     JsonObject json= test.toNGSI();
     

Use the Library

import through maven and graddle:
```xml
<dependency>
    <groupId>com.github.jmSfernandes</groupId>
    <artifactId>com.github.jmSfernandes.NGSIBaseModel</artifactId>
    <version>1.0.2</version>
</dependency>
implementation 'com.github.jmSfernandes:com.github.jmSfernandes.NGSIBaseModel:1.0.2'

Copyright [2020]

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.```

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages