1
2
3
4
5 package oshi.demo;
6
7 import com.fasterxml.jackson.core.JsonProcessingException;
8 import com.fasterxml.jackson.databind.ObjectMapper;
9
10 import oshi.SystemInfo;
11 import oshi.annotation.SuppressForbidden;
12 import oshi.hardware.CentralProcessor;
13 import oshi.hardware.GlobalMemory;
14 import oshi.hardware.HardwareAbstractionLayer;
15
16
17
18
19 public class Json {
20
21
22
23
24
25
26
27 @SuppressForbidden(reason = "Using System.out in a demo class")
28 public static void main(String[] args) {
29
30 ObjectMapper mapper = new ObjectMapper();
31
32
33 SystemInfo si = new SystemInfo();
34 HardwareAbstractionLayer hal = si.getHardware();
35
36 try {
37
38 System.out.println("JSON for CPU:");
39 CentralProcessor cpu = hal.getProcessor();
40 System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(cpu));
41
42
43 System.out.println("JSON for Memory:");
44 GlobalMemory mem = hal.getMemory();
45 System.out.println(mapper.writeValueAsString(mem));
46
47 } catch (JsonProcessingException e) {
48 System.out.println("Exception encountered: " + e.getMessage());
49 }
50 }
51 }