Using Jackson Without Annotations To Quickly Add Logging Of Object-Graphs As JSON

Normally, you have to add Annotations to your classes, if you want to serialize them with Jackson.
The following snippet shows, how you can configure Jackson in order to serialize vanilla classes without adding annotations.
This is usefull, if you want to add logging-statements, that print out graphs of objects in JSON-notation for classes, that are not prepared for serialization.


ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
String str = mapper.writeValueAsString(new Bar());

I have put together a tiny sample-project, that demonstrates the approach.
URL for cloning with GIT:
https://juplo.de/git/demos/noanno/

It can be executed with mvn spring-boot:run

Compare Two Files In Different Branches With Git

Ever wanted to do a quick diff between two different files in two different commits with git? Then read on, here’s how you can do it…

Goal

  • Compare two files in two commits with git
  • Commit may be anything denominatable by git (commit, branch, HEAD, remote-branch)
  • Name / Path may differ
  • Branch may differ

Tip

Syntax

git diff BRANCH:PATH OTHER_BRANCH:OTHER_PATH

Examples

  • Compare two different files in two different branches:

    git diff branch_a:file_a.txt branch_b:file_b.txt
    
  • Compare a file with another version of itself in another commit

    git diff HEAD:file.txt a09127a:file.txt
    
  • Same as above, but the commit is denominated by its branch:

    git diff HEAD:file.txt branchname:file.txt
    
  • Same as above, but with shortcut-syntax for the currently checked-out commit:

    git diff :file.txt branchname:file.txt
    
  • Compare a file with itself four commits ago (readable syntax):

    git diff :file.txt HEAD~4:file.txt
    
  • Compare a file with itself four commits ago (handy syntax):

    git diff :file.txt HEAD~4:file.txt
    
  • Compare a file with its latest version in the origin-repository:

    git diff :file.txt remotes/origin/master:file.txt
    
  • Compare a file with its fourth-latest version in the foo-branch of the bar-repository:

    git diff :file.txt remotes/bar/foo~4:file.txt
    

Explanation

If the path (aka object name) contains a colon (:), git interprets the part before the colon as a commit and the part after it as the path in the tree, denominated by the commit. (For more details refere to this post with tips for git show)

Cat Any File in Any Commit With Git

Ever wanted to do take a quick look at the version of some file in a different commit without checking out that commit first? Then read on, here’s how you can do it…

Goal

  • Take a quick look at a special version of a file with git withou checking out the commit first
  • Commit may be anything denominatable by git (commit, branch, HEAD, remote-branch)
  • Branch may differ
  • Pipe into another command in the shell
  • Overwrite a file with an older version of itself

Tip

Syntax

git show BRANCH:PATH

Examples

  • Show the content of file file.txt in commit a09127:

    git show a09127a:file.txt
    

    The commit can be specified with any valid denominator and may belong to any local- or remote-branch…

    • Same as above, but specify the commit relativ to the checked-out commit (handy syntax):

      git show HEAD^^^^:file.txt
      
    • Same as above, but specify the commit relativ to the checked-out commit (readable syntax):

      git show HEAD~4:file.txt
      
    • Same as above for a remote-branch:

      git show remotes/origin/master~4:file.txt
      
    • Same as above for the branch foo in repository bar:

      git show remotes/bar/foo~4:file.txt
      
  • Pipe the file into another command:

    git show a09127a:file.txt | wc -l
    
  • Overwrite the file with its version four commits ago:

    git show HEAD~4:file.txt > file.txt
    

Explanation

If the path (aka object name) contains a colon (:), git interprets the part before the colon as a commit and the part after it as the path in the tree, denominated by the commit.

  • The commit can be specified by its reference, or the name of a local or remote branch
  • The path is interpreted as absolut to the origin of the tree, denominated by the commit
  • If you want to use a relative path (i.e, current directory), prepend the path accordingly — for example ./file.
    But in this case, be aware that the path is expanded against the checked-out version and not the version, that is specified before the colon!