Avro – Unions

As a boolean:

{
	"fieldOne": "hello world",
	"fieldTwo": true,
	"foobar": {
		"boolean": false
	}
}

If value is null:

{
	"fieldOne": "hello world",
	"fieldTwo": true,
	"foobar": null
}

The above is what a field will look like as output where in Avro that field is defined as one of the Complex Types – Unions. Example below, it declares a schema which may be one of null or boolean.

{
  "type": [
    "null", 
    "boolean"
  ]
}

Convert a Map to POJO in Java

How do I convert a Map object to my other plain old Java object (POJO) without going into loops and having to write a class using Java reflection, or some other?

ANSWER

Well, yes, reflection is one but you didn’t want to do that yourself. For some good reason, I bet. It’s a good exercise if you have all the time in the world. But when faced with deadlines and having to write Unit tests for a whole class you wrote, there must be an easier way.

There is more than one way, but what I normally use is the Jackson ObjectMapper. Yes, the same one from the com.fasterxml.jackson library.

Anyway, with ObjectMapper it is pretty straightforward to do so. If I have a Person class like this:

    public class Person {
        private String firstName;
        private String lastName;
    }

My Map object will look like this:

        Map<String, Object> map = new HashMap<>();
        map.put("firstName", "Johnny");
        map.put("lastName", "Foo");

Then with ObjectMapper one can simply do this:

        ObjectMapper mapper = new ObjectMapper();
        Person person = mapper.convertValue(map, Person.class);

Alternatively, the keys in the Map may not align with the fields in the Person class. Well, I usually encounter this when working with JSON objects with lots of crazy looking field names. Something like this – NZT_Mor_First_Name__c – which I clearly don’t want my class field name to be like.

Well, we can use @JsonProperty annotation which is part of Jackson by the way and assign that our class field. The Person class will now look like:

    public class Person {

        @JsonProperty("NZT_Mor_First_Name__c")
        private String firstName;

        @JsonProperty("NZT_Mor_Last_Name__c")
        private String lastName;
    }

Again the Map will hold these values:

        Map<String, Object> map = new HashMap<>();
        map.put("NZT_Mor_First_Name__c", "Jose");
        map.put("NZT_Mor_Last_Name__c", "Yamut");

Now it will map out those weird looking key names to its corresponding class fields.

One thing to note is you might need to set ObjectMapper features such as ignoring unknown properties and make it case insensitive. Allow it a bit more room to wiggle, wiggle.

        ObjectMapper mapper = new ObjectMapper()
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                .enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES);

Should I quote Strings in YAML?

Should I or should I not? Is it required? Will it work even without quotes – single or double?

ANSWER

I’ve asked myself these questions several times. Often I end up double-quoting long strings, but not for one word ones or shorter strings. Which makes it rather inconsistent. Especially if that string contains characters like a forward slash, a colon or semi-colon. Like when you have a URL as a value. I suppose it’s out of habit.

Thing is, it is not required. Strings can be quoted or not. There is no difference. It will still work with or without it. Even with spaces in that string. YAML allows it. Below are 2 examples:

phrase: 'The quick brown fox jumps over the lazy dog.'
poem: "Hey, diddle, diddle. The cat and the fiddle. The cow jumped over the moon."

The key part of the key-value pair can also be quoted too. The YAML won’t error out. Again, it’s allowed. So this is valid:

'foo':
  "bar": foobar

Single quote or double quotes don’t matter. It can be mixed. Above examples are both acceptable.

However, not quoting is not true in all cases. There are certain instances when it is required such as when there is a colon in the key. In this case, the key needs to be quoted.

'key:': value

What about quotes in the value itself?

CORRECT - phrase1: 'The quick ''brown'' fox jumped over the lazy dog.'
WRONG- phrase2: "The quick ""brown"" fox jumped over the lazy dog."
CORRECT - phrase3: "The quick \"brown\" fox jumped over the lazy dog."
WRONG - phrase4: "The quick '"brown"' fox jumped over the lazy dog."
CORRECT - phrase6: The quick 'brown' fox jumped over the lazy dog.
CORRECT - phrase7: The quick "brown" fox jumped over the lazy dog.

Finally, what does YAML mean?

It is short for – YAML Ain’t Markup Language.

How to ignore unknown fields when parsing JSON using Jackson

In Java, there is Jackson 2 library that is very popularly used when reading JSON objects and mapping those values out to a POJO.

Happy path, as long a I have all the fields from the JSON defined in the POJO then okay. In a perfect world it will work 100%.

But there is no perfect world. The JSON object in other scenarios can contain unknown fields. These are not in my POJO. Application fails from reading that JSON to POJO.

Getting this error:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "status" (class com.example.dto.Evaluation), not marked as ignorable (6 known properties: "Book",

ANSWER

Use this Jackson annotation at a class level. This has to be configured per class. That should do it. Will ignore any unknown properties silently including nested objects.

@JsonIgnoreProperties(ignoreUnknown = true)

When you are using Jackson ObjectMapper directly, that can be configured to ignore unknown properties globally as well.

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

How to update the 3rd level of a json

I have a json file that looks like this

{
    "PL001": {
        "player_name": "Player 1",
        "player_email": "playeremail@email.com",
        "hobbies": {
          "SP001": {
            "sport": "Soccer",
            "positions": {
              "FL1":{
                "position": "Goalie"
              }
            }
          }
        }
    },
    "PL002": {
      "player_name": "Player 2",
      "player_email": "playeremail2@email.com",
      "hobbies": {
        "SP002": {
          "sport": "Hockey",
          "positions": {
            "FL2":{
              "position": "goaltender"
            }
          }
        }
      }
    }
}

What I need to do is change my positions codes to my new ones so for example my FL2 will be PLFL2

I’ve managed to get to the positions code but I’m not sure as to how I should go about pushing the new code up without loosing data.

Here is my codes

$old_code = "FL2";
$new_code = "PLFL2";


$json = json_decode(file_get_contents(storage_path('/players.json')));

$result = [];
foreach ($json as $key => $value)
{
  foreach($value->hobbies as $hobbiesCode => $hobby)
  {
    foreach ($hobby->positions as $positionCode => $position)
    {
      $positionCode = $new_code;
    }
  }
}

and that is where I get stuck. I’m not sure how to now go and update my json file with the new codes

Go to Source
Author: Aurilie