#java.lang.NoClassDefFoundError: com/fasterxml/jackson/dataformat/yaml/YAMLFactory
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
yes. and the full exception
why is the maven dependency yellow?
small sidequestion, are u using VC code ?
and line 45 of discordveri... is what ? ( no line numbers on any screenshot )
with the small section u posted its quite hard to help. But since your using a file, i'd start looking there with debug mode
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.13.0</version>
</dependency>
version might be wrong, but do u have it ?
2.15.2 is latest, did u update your maven after adding that section ?
make sure you right clicked + refreshed deps?
I mean, it seems like somehow the dependency just isn't there?
What are you using the yaml for?
Maybe we can take a stop and make Json work
Then get back to the yaml
well tbh this should work as is
hence why i want you to switch back to json for a moment, since it will rule out any issues with the yaml integration
it has a lot of odd choices in it, that's for sure
and some legacy things
File class, casts, using nodes instead of letting it deserialize to a pojo automatically,...
also catching exception
all of what i just said should be redone properly
not just that aspect
anyways. to fix ur problem at hand, it would be helpful to understand what the problem actually is
so please describe it in more detail
oh i see whats happening
hashmap.put returns a value
and thats what is being written
ObjectMapper objectMapper = new ObjectMapper();
HashMap<String, String[]> m = new HashMap<>();
m.put(k, v);
objectMapper.writeValue(new File(CLIENTS_YML_PATH), m);
try this
inside the try
.put returns the previous value associated with the key or null if there is no previous value
so when you do new HashMap<>().put(...) you get null
which is all the hint i need to say this
STOP FUCKING USING CHAT GPT TO WRITE CODE FUCK
AAAGHGHGH
objectMapper.writeValue(new File(CLIENTS_YML_PATH), Map.of(k, v));
this will work too
yeah and chat gpt doesn't know about it
since it learned java from all the shitty articles ever written and the horrible 90s codebases
also - if you don't need yaml consider using my json library
nah its good
i just made a library specifically tailored for beginners
try (var writer = Files.newBufferedWriter(Path.of(CLIENTS_YML_PATH))) {
Json.write(
writer,
Json.objectBuilder()
.put(k, Arrays.stream(v).map(Json::of).toList())
.build()
)
}
i think objectMapper.readValue(???, String[].class)
or just
actually
objectMapper.readValue(file, new TypeToken<HashMap<String, String[]>>() {})
for jackson
or just create a pojo like any sane person would do
well in this case isn't the set of keys open?
im not sure the annotation that "flattens" things
in which case u should alter the json. cause json with undefined keys is horrible bullshit
@JsonCreator
record Stuff(HashMap<String, String[]> value) {}
i think
and then read in Stuff.class
its just a shitty data model
do proper json
with well defined keys and then working with it is simple
key value pairs are fine - idk what they are using them for tho
keys are not values
bad:
{
day0: 30,
day1: 27,
day2: 35
day3: 21
}
better:
{
measurements: [
{ day: 1, temp: 30},
{ day: 2, temp: 27} ,
...
]
}
and suddenly its super easy to work with
and u can serialize in a single simple line of code with a pojo
yeah - jackson really guides you to certain json structures
since some are easy to work with, some suck
public class Main {
static String CLIENTS_YML_PATH = "test.json";
public static void main(String[] args) throws IOException {
Map<String, String[]> stuff;
try (var reader = Files.newBufferedReader(Path.of(CLIENTS_YML_PATH))) {
Json json = Json.read(reader);
stuff = JsonDecoder.object(
json,
JsonDecoder.array(JsonDecoder::string)
.map(list -> list.toArray(String[]::new))
);
}
System.out.println(stuff);
}
}
anyways
this is how you would do that with my library
where everything is uniformly painful
no, each time you try to use jackson, you mess up 🙂
you shouldn't need any annotation
depending of what you want
the only annotation you really need is JsonProperty on some fields for mapping json name with variable
right?
For this case they want a top level Map<String, String[]>
Afaik the ways to get that are
- Hash map subclass
- Type token
- Some annotation (which I think is Json creator)
no, you don't need any annotation
i think
try and see i guess
im pretty sure without the annotation it will look for
{
"value": {
"a": [ "b", "c" ],
"d": [ "e" ]
}
}
when what you want is for it to read
{
"a": [ "b", "c" ],
"d": [ "e" ]
}
it will infer it
json creator wont work either for some reason
yes that's normal
jackson really guides you to certain json structures
since some are easy to work with, some suck
but i don't think this claim is just me being bad at jackson
it's because it's a record
when you have a record, jackson except that it basically match the json, because a record represents data
there are three things you can do here :
public record StuffFlat(Map<String, String[]> value) {
@JsonCreator
public static StuffFlat from(Map<String, String[]> value) {
return new StuffFlat(value);
}
}
that works, but this doesn't
public record StuffFlat(Map<String, String[]> value) {
@JsonCreator
public StuffFlat {
}
}
(+ still doesn't with the args written/overrode)
so thats interesting and fun
it's because it's a record
when you have a record, jackson except that it basically match the json, because a record represents data
there are three things you can do here :
- Use a factory :
record Container(Map<String, String[]> values) {
@JsonCreator
public static Container foo(Map<String, String[]> values) {
return new Container(values);
}
}
- Use a class
class Container {
Map<String, String[]> values;
@JsonCreator
public Container(Map<String, String[]> values) {
this.values = values;
}
}
- Don't use a container :
Map<String, String[]> values = objectMapper.readValue(json, new TypeReference<>(){});
- Make a container
class Container extends HashMap<String, String[]> {}
More like use inheritance 🤔