'quarkus-redis-client - How to insert JSON?

How to insert JSON using quarkus-redis-client?

I tried writing the json as a String, but I don't know if it's correct.

@Singleton
public class EmployeeService {

    @Inject
    RedisClient redisClient;    

    public void insert(Employee employee) throws JsonProcessingException{
        ObjectMapper mapper = new ObjectMapper();
        String str = mapper.writeValueAsString(employee);
        redisClient.set(Arrays.asList("employee", str ));
    }

    Employee get(String key) throws JsonMappingException, JsonProcessingException {

        ///Response res = redisClient.get(key);

        String str = redisClient.get(key).toString();

        ObjectMapper mapper = new ObjectMapper();
        Employee emp = mapper.readValue(str, Employee.class);

        return emp;
    } 
    
}

source code: https://github.com/alissonmelonascimento/quarkus-app-redis



Solution 1:[1]

Redis does not support JSON out of the box. It does only support the following datatypes. So it is OK to store JSON as a string.

But if you really need JSON support, you can try installing RedisJSON module in your server.

Solution 2:[2]

Do you can use this line to persist the information at Redis, the unique thing that you need to change is to replace the " to ' in your JSON content.

redisClient.set(Arrays.asList("employee", str ));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 usuario
Solution 2 Luiz Henrique De Sousa Ribas